Note that there are some explanatory texts on larger screens.

plurals
  1. POParentheses altering semantics of function call result
    text
    copied!<p>It was noted in <a href="https://stackoverflow.com/questions/6726219/strict-standards-error">another question</a> 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:</p> <pre><code>&lt;?php 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 } foo(); </code></pre> <p>I'm trying to find anything in the documentation to <strong>explicitly</strong> 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.</p> <p>Is there anything hidden in the documentation regarding this behaviour? If not, can somebody else explain it without resorting to supposition?</p> <hr> <h3>Update</h3> <p>I first found <a href="http://www.icosaedro.it/articoli/php-syntax-ebnf.txt" rel="nofollow noreferrer">this EBNF</a> purporting to represent the PHP grammar, and tried to decode my scripts myself, but eventually gave up.</p> <p>Then, <a href="https://stackoverflow.com/questions/3609110/standalone-grammar-and-parser-for-php/3786034#3786034">using <code>phc</code></a> to generate a <code>.dot</code> file of the two <code>foo()</code> variants, I <a href="https://stackoverflow.com/questions/1494492/graphviz-how-to-go-from-dot-to-a-graph">produced</a> AST images for both scripts using the following commands:</p> <pre><code>$ yum install phc graphviz $ phc --dump-ast-dot test1.php &gt; test1.dot $ dot -Tpng test1.dot &gt; test1.png $ phc --dump-ast-dot test2.php &gt; test2.dot $ dot -Tpng test2.dot &gt; test2.png </code></pre> <p>In both cases the result was exactly the same:</p> <p><a href="https://i.stack.imgur.com/XWZMx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XWZMx.png" alt="Parse tree of snippets 1 and 2"></a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload