PHP list Keyword
Example
Assign elements of an array to variables:
<?php
list($a, $b, $c) = [1, 2, 3];
echo "$a is " . $a . "<br>";
echo "$b is "
. $b . "<br>";
echo "$c is " . $c . "<br>";
?>
Try it Yourself »
Definition and Usage
The list
keyword keyword assigns elements of an array to a list of variables.
If there are not enough elements in the array it will output a notice and assign null to the remaining variables.
Note: Since PHP 7.1.0, which elements are assigned to the variables can be selected using
arrow =>
syntax.
More Examples
Example
Choose which element is assigned to which variable:
<?php
list(2 => $a, 0 => $b, 1 => $c) = [1, 2, 3];
echo "$a is " . $a . "<br>";
echo "$b is " . $b . "<br>";
echo "$c is " . $c . "<br>";
?>
Try it Yourself »
❮ PHP Keywords
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.