Null Coalescing Assignment Operator
In the previous section you learned about the ??
comparison operator. The ??= operator that is discussed
in this section is an assignment operator that complements
?? to reduce the amount of code you need to write for
common use cases even further.
$user = $user ?? 'anonymous';
var_dump($user);
Executing the code shown above will print the output shown below:
string(9) "anonymous"
Using the ??= operator, the previous example can be
expressed without repeating $user:
$user ??= 'anonymous';
var_dump($user);
Executing the code shown above will print the same output as before.
Here is how the ??= operator works: the value of the
right-hand operand is assigned to the left-hand operator if the
left-hand operator is null. If the right-hand operator
is not null then nothing happens.