From the docs:

Coalesce equal or ??=operator is an assignment operator. If the left parameter is null, assigns the value of the right parameter to the left one. If the value is not null, nothing is done.

Example:

// The following 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.

Answer from Pavel Lint on Stack Overflow
🌐
PHP
php.net › manual › en › migration70.new-features.php
PHP: New features - Manual
It returns its first operand if it exists and is not null; otherwise it returns its second operand. <?php // Fetches the value of $_GET['user'] and returns 'nobody' // if it does not exist.
🌐
Tutorialspoint
tutorialspoint.com › home › php › php null coalescing operator
PHP Null Coalescing Operator
May 26, 2007 - Let us use the ternary operator ... set"; echo "The value of x is $var"; ?> ... The Null Coalescing Operator is represented by the "??" symbol....
Discussions

Has there been any talk of an "empty" coalesce operator?
https://github.com/phpstan/phpstan-strict-rules has a rule disallowing empty. I like the rule and although I haven't used this ruleset I try to avoid using empty in PHP. An undefined variable is empty, but how often do you want to write code that references a variable that may not exist? I basically never do except by mistake, and when I make that mistake I'd like my static analysis tools to tell me. If I've used empty where I mean $x === [] or $x === '' $x || === null then they can't do that. More on reddit.com
🌐 r/PHP
40
22
February 8, 2023
php 7.1 - Php Null coalescing operator - Stack Overflow
I am trying to understand how the null coalescing operator does really work. So, I tested many examples after reading the documentation in php.net and some posts on stackoverflow. However, I can't More on stackoverflow.com
🌐 stackoverflow.com
Does PHP 7.2 and up support null coalescing operators that nullify exceptions?
I believe you have some fundamental misunderstanding of a few things. The only way to catch exceptions is with catch. Exceptions are errors, so it wouldn't make sense for the null coalescing operator—which returns some value if the left-hand value is null—to magically suppress it. It also wouldn't make sense for the null coalescing operator to assume something is null if an exception occurred. Why would it? See here . Notice how the exception basically kills everything. That's supposed to happen because it means something went wrong, not that something went null. According to the PHP docs , "the program will terminate with a fatal error" if an unhandled exception occurs. Now look here . In this case, the exception was handled and the code continued as normal. At no point does the exception stop execution, at no point is null somehow returned. It's totally unrelated. If you're getting exceptions in general, you need to be handling them because they mean an error occurred that should not have occurred. That means either A) using catch, not to suppress them, but to handle them and do whatever needs to be done (for example, displaying a pretty error message), or B) rewriting whatever portion of your code allowed the exception to be thrown in the first place such that it will never happen again. So, to answer your two questions... no, PHP 7.2 does not support null coalescing operators that "nullify" exceptions, and it never will. However, yes, null coalescing operators are safe to use. More on reddit.com
🌐 r/PHPhelp
9
1
November 4, 2021
Here's a quick tip: you can use the null coalescing assignment operator for easy memoisation in PHP 7.4
That's right, but just for the record, this was the most concise version before 7.4: return $this->cache[$input] = $this->cache[$input] ?? $this->resolve($input); More on reddit.com
🌐 r/PHP
39
134
October 21, 2019
🌐
Reddit
reddit.com › r/php › has there been any talk of an "empty" coalesce operator?
r/PHP on Reddit: Has there been any talk of an "empty" coalesce operator?
February 8, 2023 -

I'm a big fan of null coalesce, but I want more. There are definitely times where I want to set a default value if a variable is "empty". Perl has two coalesce operators:

$num ||= 8; # Empty coalesce
$num //= 8; # null/undef coalesce

Any chance PHP will get any sort of empty coalesce operator? Right now I'm writing out:

if (empty($num)) { $num = 8; }

which is a lot more typing, not chainable, and less readable? It'd be really nice to be able to do something like:

$num = $cost ||| $prev_num ||| $default_val;

Where ||| is the operator I made up for empty coalesce.

🌐
PHP Delusions
phpdelusions.net › articles › null_coalescing_abuse
Do you abuse the null coalescing operator (and isset/empty as well)? - Treating PHP Delusions
But you are right about empty(). I believe it is due to php starting out as a duck-typed language and the lack of proper typing. Consider: function test(SomeType $var): ?array { //... } This is a bad practice as one needs to both check for null and an empty array. If the function were to instead always output an array, the need for empty() goes away. Instead a simple: if ($var === []) is all that's needed. Another one I see often is checking for null with empty(). Rather, the instanceof operator proves useful.
🌐
PHP.Watch
php.watch › articles › php-ternary-and-coalescing
Ternary and Ternary Coalescing Operator in PHP • PHP.Watch
May 27, 2020 - Null Coalescing operator calls isset() on the conditional expression, and the value will be returned.
🌐
W3Schools
w3schools.com › php › php_operators.asp
PHP Operators
PHP If PHP If Operators PHP If...Else PHP Shorthand if PHP Nested if PHP Switch PHP Match PHP Loops
Find elsewhere
🌐
Educative
educative.io › answers › what-is-a-null-coalescing-operator-in-php
What is a null coalescing operator in PHP?
The null coalescing operator is a binary operator which was introduced in PHP 7. It allows the programmers to shorten the code for checking variable values using isset function in conjunction with ternary operator or if-else statement.
🌐
Wikipedia
en.wikipedia.org › wiki › Null_coalescing_operator
Null coalescing operator - Wikipedia
October 31, 2025 - This operator differs from Perl's older || and ||= operators in that it considers definedness, not truth. Thus they behave differently on values that are false but defined, such as 0 or "" (a zero-length string): $a = 0; $b = 1; $c = $a // $b; # $c = 0 $c = $a || $b; # $c = 1 · PHP 7.0 introduced a null-coalescing operator with the ??
🌐
GeeksforGeeks
geeksforgeeks.org › php › ternary-operator-vs-null-coalescing-operator-in-php
Ternary operator vs Null coalescing operator in PHP - GeeksforGeeks
December 9, 2022 - Ternary Operator checks whether the value is true, but Null coalescing operator checks if the value is not null.
🌐
Reddit
reddit.com › r/phphelp › does php 7.2 and up support null coalescing operators that nullify exceptions?
r/PHPhelp on Reddit: Does PHP 7.2 and up support null coalescing operators that nullify exceptions?
November 4, 2021 -

Hi,

There are really two questions - at some point in programming PHP these past 2 years I started avoiding the null coalescing operator (??), but I don’t really know why since they all use 7.2 and the docs say the operator is available starting in 7.0.

Is the null coalescing operator safe in 7.2 and up?

Will it store null if there’s an exception generated, especially indexing a nested array and trying to Index on null or undefined?

Does isset() catch exceptions and return null as well? Or will it throw an exception?

Thanks!

Top answer
1 of 7
3
I believe you have some fundamental misunderstanding of a few things. The only way to catch exceptions is with catch. Exceptions are errors, so it wouldn't make sense for the null coalescing operator—which returns some value if the left-hand value is null—to magically suppress it. It also wouldn't make sense for the null coalescing operator to assume something is null if an exception occurred. Why would it? See here . Notice how the exception basically kills everything. That's supposed to happen because it means something went wrong, not that something went null. According to the PHP docs , "the program will terminate with a fatal error" if an unhandled exception occurs. Now look here . In this case, the exception was handled and the code continued as normal. At no point does the exception stop execution, at no point is null somehow returned. It's totally unrelated. If you're getting exceptions in general, you need to be handling them because they mean an error occurred that should not have occurred. That means either A) using catch, not to suppress them, but to handle them and do whatever needs to be done (for example, displaying a pretty error message), or B) rewriting whatever portion of your code allowed the exception to be thrown in the first place such that it will never happen again. So, to answer your two questions... no, PHP 7.2 does not support null coalescing operators that "nullify" exceptions, and it never will. However, yes, null coalescing operators are safe to use.
2 of 7
1
The ternary operator is a short replacement for simple if statements like this: if ($var) { } else { } Which matches whether the result of $var is true or false, whereas the null coalescing operator tests on not NULL and is a short replacement for: if (!is_null($var)) { } else { } A common gotcha in PHP is what contributes to a NULL value. You can set a string to "", and to empty() this returns true just the same as the value not defined or set to NULL. However, isset() checks to see if the variable is set and not NULL, and "" is not NULL, so it returns true, is_null does the opposite, returning "" as false. gettype() returns a string if the variable contains "" and empty() returns true in all three cases with boolean returning false in all three cases. If you know there will be an exception use break to break out of the loop before the exception occurs. You can specify the depth of the loop you wish to break out of, so a break 2; will break out of a nested loop that consists of two loops. You can test for exceptions by putting the code you suspect will cause an exception in the try block, then return the exception from the catch block and optionally, run code after from the finally block.
🌐
Ash Allen Design
ashallendesign.co.uk › blog › php-ternary-operator-and-null-coalescing-operators
The Difference Between ?: and ?? in PHP | Ash Allen Design
November 20, 2025 - Both operators have valid use cases and are valuable tools in your PHP arsenal. If you always know that a value will be available (i.e. it won't be unset), and you want to check for "truthiness", then the ternary operator (?:) is a good choice. However, if you want to handle unset variables gracefully, or you only want to check for null or unset values, then the null coalescing operator (??) might be the better option.
🌐
Benjamin Crozat
benjamincrozat.com › home › blog › php › php's double question mark, or the null coalescing operator
PHP's double question mark, or the null coalescing operator
September 28, 2025 - Use the PHP null coalescing operator (??) to read values with safe defaults, and use the null coalescing assignment (??=) to set a value only when it is missing. Keep in mind that ?? differs from ?: by checking for null or unset, not truthiness, ...
🌐
PHP
wiki.php.net › rfc › null_coalesce_equal_operator
PHP: rfc:null_coalesce_equal_operator
March 9, 2016 - If the value is not null, nothing is made. // 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';
🌐
dailycomputerscience
dailycomputerscience.com › post › php-8-null-coalescing-operator
PHP 8 Null Coalescing Operator
October 5, 2024 - The null coalescing operator (??) is a syntactic shorthand in PHP that allows you to check if a variable is null or not set and provide a default value if it is.
🌐
Stitcher
stitcher.io › blog › php-8-nullsafe-operator
PHP 8: the null safe operator | Stitcher.io
November 17, 2020 - The null coalescing operator is actually an isset call in disguise on its lefthand operand, which doesn't support short circuiting. Short circuiting also means that when writing something like this: ... Join over 14k subscribers on my mailing list: I write about PHP, programming, and keep you up to date about what's happening on this blog.
🌐
Phpbackend
phpbackend.com › blog › post › php-7-4-null-coalescing-assignment-operator
PHP 7.4 : Null Coalescing Assignment Operator :: PHP Backend Related discussions at PHPBackend.com
<?php $student_name = null; $student_name ??= 'Default Name'; var_dump($student_name); Above, Null Coalescing Assignment Operator checks if $student_name is null, if so it assign string(12) "Default Name" to it.
🌐
DEV Community
dev.to › frknasir › php-null-safe-and-null-coalescing-operators-jg2
PHP null-safe and null-coalescing operators - DEV Community
July 6, 2021 - The null-safe operator supports method while the null-coalescing operator doesn't. The null-coalescing operator supports array while the null-safe operator doesn't. Tagged with php, 100daysofcode, codenewbie, webdev.