|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2015-11-05 01:37 UTC] bwoebi@php.net
[2017-01-31 10:51 UTC] nikic@php.net
-Status: Open
+Status: Closed
-Assigned To:
+Assigned To: nikic
[2017-01-31 10:51 UTC] nikic@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 07:00:01 2025 UTC |
Description: ------------ Consider this line, where $a is an array: foreach (1 ? $a : [] as &$x) { According to the PHP documentation on ternary expressions, "Note: Please note that the ternary operator is an expression, and that it doesn't evaluate to a variable, but to the result of an expression. This is important to know if you want to return a variable by reference. The statement return $var == 42 ? $a : $b; in a return-by-reference function will therefore not work and a warning is issued in later PHP versions." The result of the ternary expression should not be an lvalue, and thus the above shouldn't work. But it actually does allow this. Test script: --------------- $a = [1, 2, 3, 4]; foreach (1 ? $a : [] as &$x) { $x += 10; } print_r($a); Expected result: ---------------- Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) Actual result: -------------- Array ( [0] => 11 [1] => 12 [2] => 13 [3] => 14 )