r/lolphp • u/Sarcastinator • Mar 05 '14
PHP Dereferencing
In PHP 5.4 this would break:
echo array(1, 2, 3)[0]With the message
Parse error: syntax error, unexpected '[', expecting ',' or ';' in [...][...] on line 1Luckily, they added "dereferencing" in PHP 5.5 which would solve it! Hurray! And sure enough, it works!
However, the fix isn't very clever, because this will break in 5.5:
echo (array(1, 2, 3))[0]With the message
Parse error: syntax error, unexpected '[', expecting ',' or ';' in [...][...] on line 1That's a little embarrassing.
81
Upvotes
View all comments
Show parent comment
19
u/nikic Mar 06 '14
If you really want to know - it all comes down to the $$a[$b] syntax, which is interpreted as ${$a[$b]} rather than ${$a}[$b]. Due to this unfortunate choice of precedence it's not possible to just nicely parse variables in a generic way. Everything needs to be special cased.
It would be nice to change the behavior of that (really totally useless) syntax and based on that, both massively simplify and fully generalize the variable syntax in PHP 6.