list()
and References
Prior to PHP 7.3, it was not possible to use the
list()
statement, as well as its []
short
form, with reference assignments.
$a = 1;
$b = 1;
$array = [$a, &$b];
$c = $array[0];
$d = &$array[1];
$b++;
var_dump($c);
var_dump($d);
Executing the code shown above prints the output shown below:
int(1)
int(2)
With PHP 7.3 and later, the array deconstruction can be expressed like so:
[$c, &$d] = $array;
Of course, the following also works:
list($c, &$d) = $array;