简体   繁体   中英

What is null coalescing assignment ??= operator in PHP 7.4

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?

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.

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.

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();
}

Null coalescing assignment operator chaining:

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

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

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

Example at 3v4l.org

Example Docs :

$array['key'] ??= computeDefault();
// is roughly equivalent to
if (!isset($array['key'])) {
    $array['key'] = computeDefault();
}

?? is a conditional operator, and it will always result in one of two values.

$a = 'previous value';
$a = arr['test'] ?? 'previous value';

??= on the other hand is an assignment operator

$a = 'previous value';
$a ??= arr['test']

If arr['test'] is null nothing happens, $a keeps 'previous value' . If arr['test'] is not null , $a will get the value out of the array.

Working fiddle .

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);
}

In PHP 7.4 you can do:

$array['value'] ??= 'someValue';

instead of

$array['value'] = $array['value'] ?? 'someValue';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM