# Null Coalescing Assignment Operator

> Section in “New and Extended Functionality › Language Features” of the book [PHP 7 Explained](https://php7explained.com/) by Sebastian Bergmann and Stefan Priebsch, published by thePHP.cc. The book covers PHP 7.0 to 7.4 and was last updated in November 2020. HTML version: <https://php7explained.com/new-and-extended-functionality/language-features/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.

``` php
$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`:

``` php
$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.

---

Previous: [Null Coalesce Operator](https://php7explained.com/new-and-extended-functionality/language-features/null-coalesce-operator/index.html.md) | Next: [Combined Comparison Operator](https://php7explained.com/new-and-extended-functionality/language-features/combined-comparison-operator/index.html.md)
