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 === '' $x || === null then they can't do that. Answer from czbz 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. $username = $_GET['user'] ?? 'nobody'; // This is equivalent to: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody'; // Coalescing can be chained: this will return the first // defined value out of $_GET['user'], $_POST['user'], and // 'nobody'.
🌐
Tutorialspoint
tutorialspoint.com › home › php › php null coalescing operator
PHP Null Coalescing Operator
May 26, 2007 - Let us use the ternary operator to check if a certain variable is set or not with the help of the isset() function, which returns true if declared and false if not. <?php $x = 1; $var = isset($x) ? $x : "not set"; echo "The value of x is $var"; ?> ... The Null Coalescing Operator is represented by the "??" symbol.
Discussions

php 7.4 - What is null coalescing assignment ??= operator in PHP 7.4 - Stack Overflow
I've just seen a video about upcoming PHP 7.4 features and saw new ??= operator. I already know the ?? operator. How's this different? More on stackoverflow.com
🌐 stackoverflow.com
PHP non-falsy null coalesce operator - Stack Overflow
i was very happy when i found out about php7's null coalesce operator. But now, in practice, I see that it's not what i thought it is: $x = ''; $y = $x ?? 'something'; // assigns '' to $y, not ' More on stackoverflow.com
🌐 stackoverflow.com
PHP short-ternary ("Elvis") operator vs null coalescing operator - Stack Overflow
Can someone explain the differences between ternary operator shorthand (?:) and null coalescing operator (??) in PHP? When do they behave differently and when in the same way (if that even happens)... 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 3, 2021
🌐
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.

🌐
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
🌐
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.
🌐
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.
Find elsewhere
🌐
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.
🌐
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 ??
🌐
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.
Top answer
1 of 14
645
  • Elvis ?: returns the first argument if it contains a "true-ish" value (see which values are considered loosely equal to true in the first line of the Loose comparisons with == table). Or the second argument otherwise

      $result = $var ?: 'default';
      // is a shorthand for 
      $result = $var ? $var : 'default';
    
  • Null coalescing ?? returns the first argument if it's set and is not null. Or the second argument otherwise

      $result = $var ?? 'default';
      // is a shorthand for 
      $result = isset($var) ? $var : 'default';
    

When your first argument is null, they're basically the same except that the null coalescing won't output an E_NOTICE when you have an undefined variable. The PHP 7.0 migration docs has this to say:

The null coalescing operator (??) has been added as syntactic sugar for the common case of needing to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.

Here's some example code to demonstrate this:

<?php

$a = null;

print $a ?? 'b'; // b
print "\n";

print $a ?: 'b'; // b
print "\n";

print $c ?? 'a'; // a
print "\n";

print $c ?: 'a'; // Notice: Undefined variable: c in /in/apAIb on line 14
print "\n";

$b = array('a' => null);

print $b['a'] ?? 'd'; // d
print "\n";

print $b['a'] ?: 'd'; // d
print "\n";

print $b['c'] ?? 'e'; // e
print "\n";

print $b['c'] ?: 'e'; // Notice: Undefined index: c in /in/apAIb on line 33
print "\n";

The lines that have the notice are the ones where I'm using the shorthand ternary operator as opposed to the null coalescing operator. However, even with the notice, PHP will give the same response back.

Execute the code: https://3v4l.org/McavC

Of course, this is always assuming the first argument is null. Once it's no longer null, then you end up with differences in that the ?? operator would always return the first argument while the ?: shorthand would only if the first argument was truthy, and that relies on how PHP would type-cast things to a boolean.

So:

$a = false ?? 'f'; // false
$b = false ?: 'g'; // 'g'

would then have $a be equal to false and $b equal to 'g'.

2 of 14
271

Ran the below on php interactive mode (php -a on terminal). The comment on each line shows the result.

var_export (false ?? 'value2');   // false
var_export (true  ?? 'value2');   // true
var_export (null  ?? 'value2');   // value2
var_export (''    ?? 'value2');   // ""
var_export (0     ?? 'value2');   // 0

var_export (false ?: 'value2');   // value2
var_export (true  ?: 'value2');   // true
var_export (null  ?: 'value2');   // value2
var_export (''    ?: 'value2');   // value2
var_export (0     ?: 'value2');   // value2

The Null Coalescing Operator ??

  • ?? is like a "gate" that only lets NULL through.
  • So, it always returns first parameter, unless first parameter happens to be NULL.
  • This means ?? is same as ( !isset() || is_null() )

Use of ??

  • shorten !isset() || is_null() check
  • e.g $object = $object ?? new objClassName();

Stacking Null Coalese Operator

        $v = $x ?? $y ?? $z; 

        // This is a sequence of "SET && NOT NULL"s:

        if( $x  &&  !is_null($x) ){ 
            return $x; 
        } else if( $y && !is_null($y) ){ 
            return $y; 
        } else { 
            return $z; 
        }

The Ternary Operator ?:

  • ?: is like a gate that lets anything falsy through - including NULL
  • Anything falsy: 0, empty string, NULL, false, !isset(), empty()
  • Same like old ternary operator: X ? Y : Z
  • Note: ?: will throw PHP NOTICE on undefined (unset or !isset()) variables

Use of ?:

  • checking empty(), !isset(), is_null() etc
  • shorten ternary operation like !empty($x) ? $x : $y to $x ?: $y
  • shorten if(!$x) { echo $x; } else { echo $y; } to echo $x ?: $y

Stacking Ternary Operator

        echo 0 ?: 1 ?: 2 ?: 3; //1
        echo 1 ?: 0 ?: 3 ?: 2; //1
        echo 2 ?: 1 ?: 0 ?: 3; //2
        echo 3 ?: 2 ?: 1 ?: 0; //3
    
        echo 0 ?: 1 ?: 2 ?: 3; //1
        echo 0 ?: 0 ?: 2 ?: 3; //2
        echo 0 ?: 0 ?: 0 ?: 3; //3

    
        // Source & Credit: http://php.net/manual/en/language.operators.comparison.php#95997
   
        // This is basically a sequence of:

 
        if( truthy ) {}
        else if(truthy ) {}
        else if(truthy ) {}
        ..
        else {}

Stacking both, we can shorten this:

        if( isset($_GET['name']) && !is_null($_GET['name'])) {
            $name = $_GET['name'];
        } else if( !empty($user_name) ) {
             $name = $user_name; 
        } else {
            $name = 'anonymous';
        }

To this:

        $name = $_GET['name'] ?? $user_name ?: 'anonymous';

Cool, right? :-)

🌐
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 3, 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.
🌐
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 Tutorial
phptutorial.net › home › php tutorial › php null coalescing operator
PHP Null Coalescing Operator
June 25, 2021 - In this tutorial, you'll learn about the PHP Null coalescing operator to assign a value to a variable if the variable doesn't exist or null.
🌐
Sipponen
sipponen.com › archives › 4328
PHP and Null Coalescing Operator | SIPPONEN.COM
October 2, 2024 - After making a huge amount of corrections with isset() function and if/else statements to handle NULL values, I started to think that what if I would write a function ensure_value($_GET['something'], 'default value') that would ensure I always would have a value what PHP8 expects, not NULL. But then I started to do a bit googling and almost gave up, until I hit to this very-very-good and easy to read article: Ternary Operator in PHP | How to use the PHP Ternary Operator | Codementor
🌐
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.
🌐
FlatCoding
flatcoding.com › home › php null coalescing operator: handling null values
PHP Null Coalescing Operator: Handling Null Values - FlatCoding
June 30, 2025 - The PHP null coalescing operator (??) checks if a variable is null or unset and sets a default value. Click to see syntax and examples.