|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2015-10-22 12:19 UTC] bwoebi@php.net
-Status: Open
+Status: Not a bug
[2015-10-22 12:19 UTC] bwoebi@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 07 17:00:02 2026 UTC |
Description: ------------ There appear to be some edge cases in how the null coalescing operator is treated with PHP references. See test scripts. Test 2 seems to be demonstrating an actual engine bug. The last two can be treated as related enhancements (?? is technically an operator forming an expression, but it would be nice to treat it as the special construct that it is to allow these use cases). IMPORTANT: Please run every script one by one to avoid one affecting the next. Test script: --------------- // Test 1. Assign by reference for existing var. $foo = 'foo'; $bar = & $foo ?? 'default'; // No error (AS EXPECTED) var_dump($bar); // string "foo" (AS EXPECTED) $bar = 'bar'; var_dump($foo); // string "bar" (AS EXPECTED) // Test 2. Assign by reference for non-existing var. Results are identical if we set $foo to NULL initially. $bar = & $foo ?? 'default'; // No error (AS EXPECTED) var_dump($bar); // NULL (*NOT* EXPECTED, string "default" expected) $bar = 'bar'; var_dump($foo); // string 'bar' (AS EXPECTED) // Test 3. Return by reference with null coalescing operator. function & foo() { $x = 123; return $x ?? 'default'; } foo(); // Notice: Only variable references should be returned by reference (NOT EXPECTED, preferably this should "just work" like assignment does). // Test 4. Pass by reference with null coalescing operator. function foo(& $x) {} $x = 123; foo($x ?? 'default'); // Fatal error: Only variables can be passed by reference (NOT EXPECTED, preferably this should "just work" like assignment does) Expected result: ---------------- See test code comments for expected results. Actual result: -------------- See test code comments for actual results.