PHP list() Function
Example
Assign variables as if they were an array:
<?php
$my_array = array("Dog","Cat","Horse");
list($a, $b, $c) = $my_array;
echo "I have several animals, a $a, a $b and a $c.";
?>
Run example »
Definition and Usage
The list() function is used to assign values to a list of variables in one operation.
Note: This function only works on numerical arrays.
Syntax
list(var1,var2...)
Parameter | Description |
---|---|
var1 | Required. The first variable to assign a value to |
var2,... | Optional. More variables to assign values to |
Technical Details
Return Value: | Returns the assigned array |
---|---|
PHP Version: | 4+ |
More Examples
Example 1
Using the first and third variables:
<?php
$my_array = array("Dog","Cat","Horse");
list($a, , $c) = $my_array;
echo "Here I only use the $a and $c variables.";
?>
Run example »
❮ PHP Array Reference