# RFC: Null Coalesce Equal Operator

- Status: Implemented (PHP 7.4)
- Authors: Midori Kocak, mtkocak@gmail.com
- Date: 2016-03-09
- Version: 0.1.0
- Original: https://wiki.php.net/rfc/null_coalesce_equal_operator
- Page: https://externals.io/rfc/null_coalesce_equal_operator
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Votes

### Approve Equal Null Coalesce Operator RFC and merge patch into master? (closed)

Yes: 37 · No: 4

Voters: aharvey (Yes), ajf (No), brianlmoon (Yes), colinodell (Yes), daverandom (Yes), davey (Yes), dmitry (Yes), dragoonis (Yes), francois (Yes), frozenfire (Yes), galvao (Yes), guilhermeblanco (Yes), jwage (Yes), klaussilveira (Yes), krakjoe (No), laruence (No), lcobucci (Yes), leigh (Yes), levim (No), lstrojny (Yes), malukenho (Yes), marcio (Yes), mariano (Yes), mbeccati (Yes), mcmic (Yes), mrook (Yes), ocramius (Yes), patrickallaert (Yes), pauloelr (Yes), pierrick (Yes), pollita (Yes), ralphschindler (Yes), stas (Yes), svpernova09 (Yes), toby (Yes), tpunt (Yes), trowski (Yes), weierophinney (Yes), yohgaki (Yes), zeev (Yes), zimt (Yes)

## Discussions on the mailing list

- [\[RFC Proposal\] Null Coalesce Equal Operator](https://externals.io/message/91593.md) — started on 2016-03-09, 21 messages
- [\[RFC\]\[Voting\] Null Coalesce Equal Operator](https://externals.io/message/91893.md) — started on 2016-03-24, 4 messages
- [\[RFC Accepted / Voting Closed\] Null Coalescing Assignment Operator ??=](https://externals.io/message/92068.md) — started on 2016-04-02, 1 messages
- [null coalesce addition assignment operator ??+=](https://externals.io/message/101620.md) — started on 2018-01-18, 12 messages
- [Coalesce assign operator (??=) implemented](https://externals.io/message/103774.md) — started on 2019-01-22, 3 messages
- [Supports to ?:= assignment operator](https://externals.io/message/112222.md) — started on 2020-11-11, 2 messages
- [??= and function calls](https://externals.io/message/120743.md) — started on 2023-07-04, 5 messages

---

## Introduction

Combined assignment operators have been around since 1970's, appearing first in the C Programming Language. For example, `$x = $x + 3` can be shortened to `$x += 3`. With PHP being a web focused language, the `??` operator is often used to check something's existence like `$username = $_GET['user'] ?? 'nobody';` However, because variable names are often much longer than $username, the use of ?? for self assignment creates repeated code, like `$this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ?? ‘value’;`. It is also intuitive to use combined assignment operator null coalesce checking for self assignment.

## Proposal

Despite `??` coalescing operator being a comparison operator, 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 made.

```php
// 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';
```

The value of right-hand parameter is copied if the left-hand parameter is null.

## Proposed PHP Version(s)

This proposed for the next PHP 7.x.

## Patches and Tests

A pull request with a working implementation, targeting master, is here: <https://github.com/php/php-src/pull/1795>

## Vote

As this is a language change, a 2/3 majority is required. A straight Yes/No vote is being held.

Voting started at 2016/03/24 16:08 and will be closed at 2016/04/02.

**Vote: Approve Equal Null Coalesce Operator RFC and merge patch into master?** — Yes: 37 · No: 4 (closed)

## References

Links to external references, discussions or RFCs

<http://programmers.stackexchange.com/questions/134118/why-are-shortcuts-like-x-y-considered-good-practice>

## Rejected Features

Keep this updated with features that were discussed on the mail lists.
