Understanding differences in parsing
In PHP 5, expressions on the right side of an assignment operation were parsed right-to-left. In PHP 7, parsing is consistently left-to-right.
How to do it...
- A variable-variable is a way of indirectly referencing a value. In the following example, first
$$foo
is interpreted as${$bar}
. The final return value is thus the value of$bar
instead of the direct value of$foo
(which would bebar
):$foo = 'bar'; $bar = 'baz'; echo $$foo; // returns 'baz';
- In the next example we have a variable-variable
$$foo
, which references a multi-dimensional array with abar key
and abaz sub-key
:$foo = 'bar'; $bar = ['bar' => ['baz' => 'bat']]; // returns 'bat' echo $$foo['bar']['baz'];
- In PHP 5, parsing occurs right-to-left, which means the PHP engine would be looking for an
$foo array
, with abar key
and abaz
. sub-key The return value of the element would then be interpreted...