Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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?

like image 657
emix Avatar asked Nov 29 '19 10:11

emix


People also ask

What is null coalescing operator in PHP?

In PHP 7, a new feature, null coalescing operator (??) has been introduced. It is used to replace the ternary operation in conjunction with isset() function. The Null coalescing operator returns its first operand if it exists and is not NULL; otherwise it returns its second operand.

What is the use of null coalescing operator?

The null-coalescing operator ?? returns the value of its left-hand operand if it isn't null ; otherwise, it evaluates the right-hand operand and returns its result.

Is there any operator in PHP which validates variable for null value?

You can use comparison operator == , === or != to check whether a variable is null or not.

What does double question mark mean in PHP?

In PHP 7, the double question mark(??) operator known as Null Coalescing Operator. It returns its first operand if it exists and is not NULL; otherwise, it returns its second operand. It evaluates from left to right.


5 Answers

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.

like image 106
Ramki Avatar answered Sep 24 '22 20:09

Ramki


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.

like image 45
Pavel Lint Avatar answered Oct 20 '22 15:10

Pavel Lint


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();
}
like image 42
Nicholas Betsworth Avatar answered Oct 20 '22 17:10

Nicholas Betsworth


Null coalescing assignment operator chaining:

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

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

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

Example at 3v4l.org

like image 28
5d18 Avatar answered Oct 20 '22 17:10

5d18


Example Docs:

$array['key'] ??= computeDefault();
// is roughly equivalent to
if (!isset($array['key'])) {
    $array['key'] = computeDefault();
}
like image 2
Dmitry Avatar answered Oct 20 '22 17:10

Dmitry