PHP 7.4: Null Coalescing Assignment
Preface
With PHP 7.4 upcoming, it’s time to start exploring some of the new features that will be arriving alongside it. Here we cover the enhancements around the null coalescing operator, namely the introduction of the null coalescing assignment operator. (Sometimes referred to as the “null coalesce equal operator”)
Basics
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:
1
$data['username'] = (isset($data['username']) ? $data['username'] : 'guest');
When PHP 7 was released, we got the ability to instead write this as:
1
$data['username'] = $data['username'] ?? 'guest';
Now, however, when PHP 7.4 gets released, this can be simplified even further into:
1
$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.
So for example, this code from before PHP 7 could only be optimised once using the null coalescing operator, and not the assignment operator:
1
$username = (isset($_SESSION['username']) ? $_SESSION['username'] : 'guest');
becomes…
1
$username = $_SESSION['username'] ?? 'guest';