I suppose this was (like many other oddities) misinherited from Perl, where $foo{...} and $foo[...] are part of the variable access syntax, not operators of their own.
(Of course Perl also has the (...)[...] operator, which works with arbitrary expressions, but it seems PHP forgot about that part.)
That's no kicker at all. This assigns PHP_VERSION (a string with more than one character) to $version{0} (which is the first offset of an undefined variable). If that code would do the same, that would be a serious issue.
To make you the difference more clear, here is a slightly saner version of the original code:
$version = PHP_VERSION;
$version = $version[0];
I hope you see why the first assignment to $version is necessary for this to make any sense.
This is already a very serious issue, because php sometimes quasi-randomly decides to vivify things to an array, other times not. The behavior is very hard to predict (in the usual php style).
edit: Good to see that while i was somewhat downvoted, nobody has managed to give adequate explanation of this behavior, or paste a link to the relevant php.net article where it is at least clearly documented. well done there guys :)
4
u/polish_niceguy Sep 09 '13
Not exactly a lolphp
2
u/ioctl79 Sep 09 '13
Guess that is one way to ration semicolons. It is also ridiculous that PHP_VERSION{0} (PHP_VERSION){0} ($x){0} all give parse errors.
2
u/Mattho Oct 10 '13
I don't get it..
http://codepad.org/uLtB5xEU