39

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?

emix
  • 14,448
  • 10
  • 62
  • 82

6 Answers6

37

In PHP 7 this was originally released, allowing a developer to simplify an isset() check combined with a ternary operator. For example, before PHP 7, we might have this code:

$data['username'] = (isset($data['username']) ? $data['username'] : 'guest');

When PHP 7 was released, we got the ability to instead write this as:

$data['username'] = $data['username'] ?? 'guest';

Now, however, when PHP 7.4 gets released, this can be simplified even further into:

$data['username'] ??= 'guest';

One case where this doesn't work is if you're looking to assign a value to a different variable, so you'd be unable to use this new option. As such, while this is welcomed there might be a few limited use cases.

Ramki
  • 910
  • 3
  • 17
  • 15
    Give credit where credit is due. This entire answer is copy/pasted from [here](https://codular.com/php-7-4-null-coalescing-assignment-operator). – Adrian Wiik Jul 06 '20 at 06:47
  • 2
    "here" hyperlink in @AdrianWiik's comment is now broken. – mickmackusa Oct 14 '20 at 04:20
  • 2
    DAMN! It wasn’t renewed in time and expired on 2020-09-21, just 22 days ago. The sharks want $5,000 for it. Here’s the last known archive of the post, from 1 June 2020: https://web.archive.org/web/20200701060341/https://codular.com/php-7-4-null-coalescing-assignment-operator – Theodore R. Smith Oct 14 '20 at 06:03
  • He gave it up on purpose!! https://michael.wright.uk/sunsetting-codular-com – Theodore R. Smith Oct 14 '20 at 06:05
  • 1
    Get it from the array, or make a new one, put it into the array, and assign it to the variable as well. :-o ``$item = $this->items[$name] ??= new Item($name);`` – GDmac Dec 01 '20 at 20:01
29

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
  • 2,972
  • 1
  • 16
  • 17
  • 5
    Also looks like we found a typo in the official docs. `The folloving lines...` – Pavel Lint Nov 29 '19 at 10:16
  • 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
6

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,635
  • 18
  • 24
5

Null coalescing assignment operator chaining:

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

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

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

Example at 3v4l.org

emix
  • 14,448
  • 10
  • 62
  • 82
5d18
  • 51
  • 1
  • 2
  • 1
    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
  • 3,457
  • 3
  • 19
  • 33
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
  • 8,906
  • 13
  • 76
  • 116