Short List Syntax
The list()
statement allows us to assign variables
as if they were an array:
$data = ['foo', 'bar', 'barbara'];
list($first, $second, $third) = $data;
var_dump($first);
var_dump($second);
var_dump($third);
Executing the code shown above will print the output shown below:
string(3) "foo"
string(3) "bar"
string(7) "barbara"
PHP 5.4 introduced the short array syntax, allowing the
construction of arrays using the []
notation instead of
using array()
. The fact that list()
still
had to be used for unpacking array elements into variables was
considered unesthetic by some.
The syntax shown below, which is supported as of PHP 7.1, is equivalent to the one shown above:
$data = ['foo', 'bar', 'barbara'];
[$first, $second, $third] = $data;