Bug #78387 [Nab]: references won't work for coalesce result

From: Date: Thu, 08 Aug 2019 22:22:37 +0000
Subject: Bug #78387 [Nab]: references won't work for coalesce result
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-222146@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=78387&edit=1 ID: 78387 User updated by: mpatnode at popsugar dot com Reported by: mpatnode at popsugar dot com Summary: references won't work for coalesce result Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: any PHP Version: 7.4.0beta2 Block user comment: N Private report: N New Comment: You suggest a warning should of been issued for asking for a reference from an expression? We look for messages at the Notice level and didn't see anything in this case. Previous Comments: ------------------------------------------------------------------------ [2019-08-08 21:48:43] requinix@php.net This is documented, more or less, with the ?? operator itself. https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.coalesce > Note: Please note that the null coalescing 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 $foo ?? $bar; in a return-by-reference function will therefore not work and a > warning is issued. Since $foo ?? [] is an expression, $bar will not be a reference to a value inside $foo but to a value inside the *copy of* $foo that was "returned" by the expression. It's legal syntax, just not what you wanted it to do. ------------------------------------------------------------------------ [2019-08-08 21:17:15] mpatnode at popsugar dot com Description: ------------ Not sure if this is a bug, but the behavior is unexpected. If a coalesce result is used in a foreach with a reference, the reference is ignored. If it's not legal syntax, then the reference should generate a warning or notice. Behaved the same on every PHP version I tested. Test script: --------------- $foo = ['bar1' => 1, 'bar2' => 2]; foreach ($foo ?? [] as &$bar) { $bar = 3; } var_dump($foo) Expected result: ---------------- array(2) { 'bar1' => int(3) 'bar2' => int(3) } Actual result: -------------- array(2) { 'bar1' => int(1) 'bar2' => int(2) } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=78387&edit=1

« previous php.bugs (#222146) next »