Home >>PHP Array Functions >PHP list() Function

PHP list() Function

PHP list() Function

PHP list() function is used to assign the array values to multiple variables at a time. It works only on the numerical arrays. The first element in the given array is assigned to the first variable passed and the second element to the second variable and so on, till the number of variables passed in parameter.  It accepts a list of variables as parameter separated by spaces.

Syntax:

list($var1, $var2, ...);

Parameter Values

Parameter Description
var1 This is a required parameter. It defines the first variable to assign a value to.
var2,... This is an optional parameter. It defines more variables to assign values to.

Here is an example of list() function in PHP:

<html>
<body>
<?php
$array = array("New York","Paris","Prague","San Francisco","Rome");
list($a, $b, $c, $d, $e) = $array;
echo "I love travelling and a want to travel $a, $b, $c, $d, and $e someday.";
?>
</body>
</html>
Output:
I love travelling and a want to travel New York, Paris, Prague, San Francisco, and Rome someday.

Example 2:

<html>
<body>
<?php
$array = array("New York","Paris","Prague","San Francisco","Rome");
list($a, $b, , $d) = $array;
echo "I love travelling and a want to travel $a, $b, and $d someday.";
?>
</body>
</html>
Output:
I love travelling and a want to travel New York, Paris, and San Francisco someday.

No Sidebar ads