Assign values to multiple variables from an array-like structure.
<?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.”; ?> |
The list()
function assigns values to a set of variables in a single operation.
Note: Before PHP 7.1, this function only worked with numerical arrays.
list(var1, var2, …) |
Parameter |
Description |
var1 |
Required. The first variable to assign a value to |
var2,… |
Optional. Additional variables to assign values to |
Return Value: |
Returns an array with the assigned values |
PHP Version: |
4+ |
Assign values to 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.”; ?> |