Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Resolving 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...



Array 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


I usually write it as

$xml = reset(getXpathWhatever());

which returns the first element from array.




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: