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. Answer from ClosetLink on reddit.com
🌐
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.
🌐
Stitcher
stitcher.io › blog › php-8-nullsafe-operator
PHP 8: the null safe operator | Stitcher.io
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.
🌐
Markshust
markshust.com › 2017 › 08 › 25 › php-7s-null-coalesce-operator-usage-objects-and-arrays
PHP 7's null coalesce operator usage with Objects and Arrays | Mark Shust
<?php class Foo {} $foo = new Foo; echo $foo->b->c ?? 'baz'; And it even works if the object isn’t even defined. This script runs without errors: ... Another use for null coalesce is when working with arrays, and things like foreach loops. For example, let’s say we want to iterate over an array, but are not sure if this array exists.
🌐
Rip Tutorial
riptutorial.com › null coalescing operator (??)
PHP Tutorial => Null Coalescing Operator (??)
Null coalescing is a new operator introduced in PHP 7. This operator returns its first operand if it is set and not NULL.
🌐
PHP Tutorial
phptutorial.net › home › php tutorial › php null coalescing operator
PHP Null Coalescing Operator
June 25, 2021 - <?php $name = $_POST['name'] ?? 'John';Code language: PHP (php) In this example, the ?? is the null coalescing operator. It accepts two operands. If the first operand is null or doesn’t exist, the null coalescing operator returns the second operand.
🌐
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....
🌐
Educative
educative.io › answers › what-is-a-null-coalescing-operator-in-php
What is a null coalescing operator in PHP?
The null coalescing operator (??) was introduced in PHP 7. In PHP, the null coalescing operator (??) returns its first operand if the first operand exists and it 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 1, 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.
Find elsewhere
🌐
SitePoint
sitepoint.com › php
Null Coalescing Operator equivalent for object property - PHP - SitePoint Forums | Web Development & Design Community
February 10, 2022 - Hello. I’m currently following through the PHP Novice to Ninja book, and adapting it for a blog website. I have potential problem if a user account is deleted and I’m wondering how I might do the equivalent of a null coalescecing operator on the following name property of a comment object:
🌐
PHP
wiki.php.net › rfc › null_coalesce_equal_operator
PHP RFC: Null Coalescing Assignment Operator
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';
🌐
Linux Hint
linuxhint.com › null_coalescing_php
How to use PHP Null Coalescing Operator – Linux Hint
The null coalescing operator can be used with two or more variables. In this example, the operator is used to check the values of different variables. <?php //Define two variables $var1 = 'This is the first string value.'; $var3 = 'This is the third string value.'; $var4 = NULL; $var5 = 'This is the fifth string value.'; $var6 = ''; //Check the value of the variables $result1 = $var1 ??
🌐
PHP.Watch
php.watch › versions › 8.0 › null-safe-operator
Null-safe operator - PHP 8.0 • PHP.Watch
The null-safe operator allows reading the value of property and method return value chaining, where the null-safe operator short-circuits the retrieval if the value is null, without causing any errors.
🌐
GitHub
github.com › phpstan › phpstan › issues › 3283
Null Coalescing Operator with objects' attributes · Issue #3283 · phpstan/phpstan
There is no error in this code, PHP isset is working with nested attributes isset($abc->def->ghi) (https://www.php.net/manual/fr/function.isset.php#86313) and $var ?? $default is only a shortcut to isset($var) ?
🌐
FlatCoding
flatcoding.com › home › php null coalescing operator: handling null values
PHP Null Coalescing Operator: Handling Null Values - FlatCoding
June 30, 2025 - The null coalescing operator (??) was added in PHP 7. It checks if a variable is set and not null. If it is, it uses that value. If not, it uses a default value. The basic syntax looks like this: $variable = $value ??
🌐
Medium
medium.com › @prevailexcellent › mastering-null-safety-in-php-8-a-comprehensive-guide-to-using-the-null-safe-operator-47835ba1140b
Mastering Null Safety in PHP 8: A Comprehensive Guide to Using the Null Safe Operator | by Chimeremze Prevail Ejimadu | Medium
June 14, 2023 - Use the null coalescing operator when you want to provide a default value or handle fallback scenarios for null values. Use the null safe operator when you want to safely access properties and invoke methods on potentially null objects without ...
🌐
Cylab
cylab.be › blog › 479 › the-null-php-cheatsheet-null-coalescing-and-nullsafe-operators
The null PHP cheatsheet : null coalescing and nullsafe operators | cylab.be
The null coalescing operator ?? was introduced in PHP 7 and allows to avoid heavy constructions with if (isset(...)). The null coalescing operator returns the first operand if it is not null, and otherwise the second operand. $username = $_GET['username'] ?? 'nobody'; // is equivalent to: $username ...
🌐
Archybold
archybold.com › blog › post › trying-get-property-x-non-object-null-coalescing-operator-php
"Trying to get property 'x' of non-object" With Null Coalescing Operator in PHP | The Blog | archybold.com
Quick one here but today, thanks to Tez on Stack Overflow, I discovered that PHP's null coalescing operator (the double question mark operator) can fail when you use a function at any part of the statement. Here's the example in Laravel I had. I was trying to find the ID of a related object and guarantee that it will be set to null otherwise:
🌐
Stitcher
stitcher.io › blog › shorthand-comparisons-in-php
Shorthand comparisons in PHP | Stitcher.io
The null coalescing operator is used to provide default values instead of null · The spaceship operator is used to compare two values