Organizational Research By

Surprising Reserch Topic

parentheses altering semantics of function call result

parentheses altering semantics of function call result  using -'php,php-internals'

It was noted in another question that wrapping the result of a PHP function call in parentheses can somehow convert the result into a fully-fledged expression, such that the following works:

error_reporting(E_ALL | E_STRICT);

function get_array() {
   return array();

function foo() {
   // return reset(get_array());
   //              ^ error: "Only variables should be passed by reference"

   return reset((get_array()));
   //           ^ OK


I'm trying to find anything in the documentation to explicitly and unambiguously explain what is happening here. Unlike in C++, I don't know enough about the PHP grammar and its treatment of statements/expressions to derive it myself.

Is there anything hidden in the documentation regarding this behaviour? If not, can somebody else explain it without resorting to supposition?


I first found this EBNF purporting to represent the PHP grammar, and tried to decode my scripts myself, but eventually gave up.

Then, using phc to generate a .dot file of the two foo() variants, I produced AST images for both scripts using the following commands:

$ yum install phc graphviz
$ phc --dump-ast-dot test1.php >
$ dot -Tpng > test1.png
$ phc --dump-ast-dot test2.php >
$ dot -Tpng > test2.png

In both cases the result was exactly the same:


asked Sep 21, 2015 by devkumargupta
0 votes

Related Hot Questions

2 Answers

0 votes
answered Sep 21, 2015 by sachin valanju
0 votes
answered Sep 21, 2015 by shikhar jain