PHP list() Function
Example
Assign values from the array to some variables:
<?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."; ?>
Definition and Usage
The list() function is used to assign values to a group of variables in one operation.
Note:This function is only used for arrays with numeric indices and assumes that numeric indices start from 0.
Syntax
list(var1,var2...)
Parameter | Description |
---|---|
var1 | Required. The first variable to assign. |
var2,... | Optional. More variables to assign. |
Description
list() function assigns values from the elements of an array to a group of variables.
Note, with array() Similarly, list() is actually a language structure, not a function.
Technical Details
Return Value: | Return the assigned array. |
PHP Version: | 4+ |
More Examples
Example 1
Use the first and third variables:
<?php $my_array = array("Dog","Cat","Horse"); list($a, , $c) = $my_array; echo "I used $a and $c variables here only."; ?>