php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70768 Null coalescing operator inconsistencies
Submitted: 2015-10-22 12:07 UTC Modified: 2015-10-22 12:19 UTC
From: phplists at stanvassilev dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 7.0.0RC5 OS: All
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: phplists at stanvassilev dot com
New email:
PHP Version: OS:

 

 [2015-10-22 12:07 UTC] phplists at stanvassilev dot com
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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-10-22 12:19 UTC] bwoebi@php.net
-Status: Open +Status: Not a bug
 [2015-10-22 12:19 UTC] bwoebi@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

I think you're misparsing the expression.

& $foo ?? 'default'
isn't a valid expression by itself, only
$bar = & $foo
is.

Hence the code is parsed the only possible way, namely:
($bar = &$foo) ?? 'default';
which basically just will assign the reference of $foo to $bar.

In general also, the null-coalesce returns an expression, which doesn't have the same semantics as a variable. Hence is an invalid return value on by-ref functions nor can it be passed as by-ref arg.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 18:00:02 2026 UTC