I’ve searched around about this but have only found things along the lines of “here’s a trick that works” but never an explanation.
In the following code:
error_reporting(-1); ini_set("display_errors", 1); end(array_flip($_GET)); end((array_flip($_GET)));
the first call to
end() results in a notice:
Strict Standards: Only variables should be passed by reference in /home/john/www/test/dp.php on line 3
The next one, with
array_flip($_GET) enclosed in
() doesn’t generate any complaints.
I understand the reason for the notice, what Strict Standards are, what ‘pass by reference’ means, and that
end() accepts a reference to an array as its argument. (It must, since it affects the original.) My concern is about why it works. Do the surrounding
() create a temporary variable, or does this just trick the PHP parser in some way? If it does create a temporary variable (and does so by design), then this should be safe to use, but if it’s a hack to trick the parser then it will probably break in future versions of PHP. (I happen to be on PHP Version 5.4.36-0+deb7u1)
Please note that I’m not certain that I like this construction even if it is safe (it’s easy to not see it, and other people may not know why it’s there), but it is often convenient and easy to read when functions can be strung together like this.
This works out to an implementation detail, as discussed elsewhere on StackOverflow.
Summarizing: this behavior should in fact be considered a bug.Do not rely on it. In fact, thanks to this RFC, this trick will no longer work once PHP 7 is released.