3 ms·
php -r 'array("a","b")[0];' results in a parse error.
by jcampbell1 15y ago
php -r 'array("a","b")[0];' results in a parse error.
- gburt 15y agoResolving arrays like that causes parse errors in a range of contexts. Anything that is a "function" cannot also pick one array key... leaving a lot of XML-parsing code that looks like $xml = getXpathWhatever(); $xml = $xml[0]; Stranger yet, is that it works perfectly fine with resolving objects... for example $xml = funct()->something->somethingelse()->a Works fine...
- birken 15y agoArray brackets after function calls are fixed in PHP 5.4: ~/php_source/php-src-5.3$ ./sapi/cli/php -r "function a() { return array(1,2); } print a()[0];" Parse error: syntax error, unexpected '[' in Command line code on line 1 ~/php_source/php-src-5.4$ ./sapi/cli/php -r "function a() { return array(1,2); } print a()[0];" 1
- z92 15y agoI usually write it as $xml = reset(getXpathWhatever()); which returns the first element from array.