68

I've just seen a video about upcoming PHP 7.4 features and saw this new ??= operator. I already know the ?? operator.

How's this different?

Mike Doe
  • 16,349
  • 11
  • 65
  • 88

6 Answers6

50

From the docs:

Coalesce equal or ??=operator is an assignment operator. If the left parameter is null, assigns the value of the right paramater to the left one. If the value is not null, nothing is done.

Example:

// The folloving lines are doing the same
$this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ?? 'value';
// Instead of repeating variables with long names, the equal coalesce operator is used
$this->request->data['comments']['user_id'] ??= 'value';

So it's basically just a shorthand to assign a value if it hasn't been assigned before.

Community
  • 1
  • 1
Pavel Lint
  • 3,252
  • 1
  • 18
  • 18
  • 7
    Also looks like we found a typo in the official docs. `The folloving lines...` – Pavel Lint Nov 29 '19 at 10:16
  • 1
    it's not 100% exact that the 2 lines are doing "the same", in the second case the left-hand side is only evaluated once, so it's more efficient – the_nuts Apr 14 '20 at 09:44
  • *'If the left parameter is null, assigns (...)'*. Don't you think it should be corrected to: *'If the left parameter is null **OR is not set**, assigns (...)'* ? – Dawid Ohia Dec 09 '22 at 10:47
8

The null coalescing assignment operator is a shorthand way of assigning the result of the null coalescing operator.

An example from the official release notes:

$array['key'] ??= computeDefault();
// is roughly equivalent to
if (!isset($array['key'])) {
    $array['key'] = computeDefault();
}
Nicholas Betsworth
  • 1,707
  • 19
  • 24
7

Null coalescing assignment operator chaining:

$a = null;
$b = null;
$c = 'c';

$a ??= $b ??= $c;

print $b; // c
print $a; // c

Example at 3v4l.org

Mike Doe
  • 16,349
  • 11
  • 65
  • 88
5d18
  • 71
  • 1
  • 2
  • 3
    That example can be handled easily by the null coalescing operator `??` because all variables are defined. The point of the null coalescing *assignment* operator is that it will work with undefined variables. So you can remove the first two lines which assign nulls to $a and $b and the code will still work. – pbarney Jun 30 '20 at 22:22
2

Example Docs:

$array['key'] ??= computeDefault();
// is roughly equivalent to
if (!isset($array['key'])) {
    $array['key'] = computeDefault();
}
Dmitry Leiko
  • 3,970
  • 3
  • 25
  • 42
0

You can use this to initialize variables during a loop's first iteration. But beware!

$reverse_values = array();
$array = ['a','b','c']; // with [NULL, 'b', 'c'], $first_value === 'b'
foreach($array as $key => $value) {
  $first_value ??= $value; // won't be overwritten on next iteration (unless 1st value is NULL!)
  $counter ??= 0; // initialize counter
  $counter++;
  array_unshift($reverse_values,$value);
}
// $first_value === 'a', or 'b' if first value is NULL
// $counter === 3
// $reverse_values = array('c','b','a'), or array('c','b',NULL) if first value is null

If the first value is NULL, then $first_value will be initialized to NULL and then overwritten by the next non-NULL value. If the array has a lot of NULL values, $first_value will end up either as NULL or the first non-NULL after the last NULL. So this seems like a terrible idea.

I would still prefer doing something like this mainly because it's more clear, but also because it works with NULL as an array value:

$reverse_values = array();
$array = ['a','b','c']; // with [NULL, 'b', 'c'], $first_value === NULL
$counter = 0;
foreach($array as $key => $value) {
  $counter++;
  if($counter === 1) $first_value = $value; // does work with NULL first value
  array_unshift($reverse_values,$value);
}
Buttle Butkus
  • 9,206
  • 13
  • 79
  • 120
0

It can roughly be translated as "$a defaults to $b", like this:

$page ??= 1;            //  If page is not specified, start at the beginning
$menu ??= "main";       //  Default menu is the main menu
$name ??= "John Doe";   //  Name not given --> use John Doe

A long-awaited tool in the world of PHP.
Before PHP 7.4, we did this with a function:

function defaultOf(&$var, $value) {
    if(is_null($var)) $var=$value;
}

// Now the 3 statements above would look like:

defaultOf( $page, 1 );
defaultOf( $menu, "main" );
defaultOf( $name, "John Doe" );

(I still use it because it's more readable.)

dkellner
  • 8,726
  • 2
  • 49
  • 47