• 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'.

Answer from MasterOdin on Stack Overflow
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

        x ?? 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 : $y to y
  • shorten if(!$x) { echo $x; } else { echo $y; } to echo 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? :-)

๐ŸŒ
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.
๐ŸŒ
Stitcher
stitcher.io โ€บ blog โ€บ shorthand-comparisons-in-php
Shorthand comparisons in PHP | Stitcher.io
The ternary operator is used to shorten if/else structures ยท The null coalescing operator is used to provide default values instead of null
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ language.operators.comparison.php
PHP: Comparison - Manual
Chaining of short-ternaries (?:), however, is stable and behaves reasonably. It will evaluate to the first argument that evaluates to a non-falsy value. Note that undefined values will still raise a warning. ... <?php echo 0 ?: 1 ?: 2 ?: 3, PHP_EOL; //1 echo 0 ?: 0 ?: 2 ?: 3, PHP_EOL; //2 echo 0 ?: 0 ?: 0 ?: 3, PHP_EOL; //3 ?> Another useful shorthand operator is the "??" (or null coalescing) operator.
๐ŸŒ
PHP.Watch
php.watch โ€บ articles โ€บ php-ternary-and-coalescing
Ternary and Ternary Coalescing Operator in PHP โ€ข PHP.Watch
May 27, 2020 - It is now required to use braces to make the intent clear if you absolutely have to use ternary/coalescing operators. Null Coalescing Assignment operator is relatively new in PHP (added in PHP 7.4), so you code might not work in older PHP versions if you decide to use that operator.
๐ŸŒ
Mike Street
mikestreety.co.uk โ€บ blog โ€บ php-ternary-and-null-coalescing-operators
PHP Ternary and null coalescing operators - Mike Street - Lead Developer and CTO
August 10, 2023 - I write PHP on a daily basis and often, as with most programming, need to check if something is there or not, or if it is what I expect. Beyond if statements, there are ternary operators. Things like the Elvis (?:) operator or the Null coalescing operator (??) can be used to make your code leaner, cleaner and easier to read.
๐ŸŒ
MaiNetCare GmbH
mainetcare.de โ€บ en โ€บ techdoc โ€บ ternary-operator-versus-null-coalescing-operator-versus-null-coalescing-assignment-operator-explained-by-katzen
Ternary Operator ?: versus Null Coalescing Operator ?? versus Null Coalescing Assignment Operator ??= explained by cats - MaiNetCare GmbH
April 14, 2025 - $alter = 3; // Age of the cat in years $weight = 6; // Weight of the cat in kilograms // Determination of food preference with a nested ternary operator $FoodPreference = $alter < 2 ? ($weight < 3 ? 'Young animal wet food (very expensive)' : 'Young animal dry food' : ($weight < 4 ? 'Adult wet food' : 'Adult dry food'); echo "This cat prefers $ food preference.";Code language: PHP (php) The null coalescing operator ("coalescing" = "merging") checks for the presence of a value and is used with the syntax Expression1 ??
๐ŸŒ
Remicorson
remicorson.com โ€บ the-null-coalescing-operator-and-the-ternary-operator-in-php
The Null Coalescing Operator ?? and The Ternary Operator ?: in ...
Those two operators give a way to write more concise code and shorten expressions used in PHP. ... Basically, this operator will return the first non NULL value.
Find elsewhere
๐ŸŒ
Genijaho
genijaho.dev โ€บ blog โ€บ refactoring-1-using-ternary-and-null-coalescing-operators-in-php
Refactoring #1: Using ternary and null coalescing operators in PHP
It can be made shorter, however, by replacing the if conditions with the ternary operator ?:. The ternary operator will return the left-hand value if it evaluates as true, otherwise, it will return the right-hand side.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ home โ€บ php โ€บ php null coalescing operator
PHP Null Coalescing Operator
May 26, 2007 - It acts as a convenient shortcut 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. Below is the syntax for using the null coalescing operator โˆ’
๐ŸŒ
Linux Hint
linuxhint.com โ€บ null_coalescing_php
How to use PHP Null Coalescing Operator โ€“ Linux Hint
The null coalescing operator is faster than the ternary operator. The null coalescing operator is used in the following examples. Example 1: Using the null coalescing operator between two variables ยท The null coalescing operator can be used with two or more variables.
๐ŸŒ
Designcise
designcise.com โ€บ web โ€บ tutorial โ€บ whats-the-difference-between-null-coalescing-operator-and-ternary-operator-in-php
PHP ?? vs. ?: โ€“ What's the Difference? - Designcise
June 6, 2021 - ?: (Elvis Operator) The elvis operator (?:) is actually a name used for shorthand ternary (which was introduced in PHP 5.3). It has the following syntax: // PHP 5.3+ expr1 ?: expr2; This is equivalent to: expr1 ? expr1 : expr2; ?? (Null Coalescing Operator) The null coalescing operator (??) was introduced in PHP 7, and it has the following syntax: // PHP 7+ $x ?? $y; This is equivalent to: isset($x) ? $x : $y; ?: vs.
๐ŸŒ
Steemit
steemit.com โ€บ utopian-io โ€บ @gentlemanoi โ€บ php7-how-to-use-null-coalescing-operator
PHP7 : How to use Null Coalescing Operator โ€” Steemit
February 8, 2018 - So in this example, it will output 'String D' because all the previous variables are UNDEFINED. The difference between this two operators is that the null coalescing operator evaluates the first operand if it is null. It's like using the isset() function. While the ternary operator evaluates ...
๐ŸŒ
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 - If you always know that a value ... 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....
๐ŸŒ
Reddit
reddit.com โ€บ r/php โ€บ using ternary and null coalescing operators in php
r/PHP on Reddit: Using ternary and null coalescing operators in PHP
July 17, 2021 - Great question, I did it the same way at first. But what if you get an empty string from $addressArray['foo']? The ?? would return it since it's not null. And chaining the ternary operator wasn't a good idea either, precedence issues up to PHP 7.4, but fixed on 8.0.
๐ŸŒ
Experts Exchange
experts-exchange.com โ€บ questions โ€บ 29125140 โ€บ Null-Coalescing-Operator-vs-Ternary-Operator-in-PHP.html
Solved: Null Coalescing Operator vs. Ternary Operator in PHP | Experts Exchange
November 11, 2017 - Select allOpen in new window I'm learning towards the ternarary operator because everyone knows that (it's not unique to PHP) and also because it is supported by older versions of PHP. The only advantage I see to the Null Coalescing Operator is that it is less code.
๐ŸŒ
PHP Tutorial
phptutorial.net โ€บ home โ€บ php tutorial โ€บ php null coalescing operator
PHP Null Coalescing Operator
June 25, 2021 - The null coalescing operator (??) is a syntactic sugar of the ternary operator and isset().
๐ŸŒ
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 - Quick contrast: the shorthand ternary (Elvis) operator ?: checks truthiness, while ?? checks only for null or unset and avoids notices for undefined keys. PHP 7.4 introduced a new shortcut, ??= (double question mark equals), also called the ...
๐ŸŒ
sqlpey
sqlpey.com โ€บ php โ€บ php-ternary-vs-null-coalescing-operator-differences
PHP Ternary ?: vs Null Coalescing ?? Operator Key Differences
November 4, 2025 - ANS: The null coalescing operator (??) tests if the left operand is set and not null, equivalent to isset(). The shorthand ternary operator (?:) tests if the left operand evaluates to a truthy value, like a standard ternary operator.