In this article, you will learn how to assign values to multiple variables at a time. The list() function is used to pick and assign values from the array to a list of variables in a single operation.
What is the syntax of the list() function in PHP?
list(var1, var2, ...)
Parameters | Details |
---|---|
var1 | The first variable in the list to assign value to – Required |
var2,… | More variables in the list to assign value to – Optional |
Examples of list() function
Example 1. Assign values to the variables from an array.
<?php
$arr= array("a","b","c");
list($x, $y, $z) = $arr;
echo "Values of variables are $x, $y and $z";
?>
Example 2. Another example of the list function.
<?php
$arr= array("a","b","c");
list($x,,$z) = $arr;
echo "Values of variables are $x and $z";
?>