It's the "null coalescing operator", added in php 7.0. The definition of how it works is:

It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.

So it's actually just isset() in a handy operator.

Those two are equivalent1:

bar ?? 'something';
$foo = isset(bar : 'something';

Documentation: http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.coalesce

In the list of new PHP7 features: http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op

And original RFC https://wiki.php.net/rfc/isset_ternary


EDIT: As this answer gets a lot of views, little clarification:

1There is a difference: In case of ??, the first expression is evaluated only once, as opposed to ? :, where the expression is first evaluated in the condition section, then the second time in the "answer" section.

Answer from michalhosna on Stack Overflow
🌐
PHP
php.net › manual › en › language.operators.php
PHP: Operators - Manual
An operator is something that takes one or more values (or expressions, in programming jargon) and yields another value (so that the construction itself becomes an expression).
🌐
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
Discussions

What does double question mark (??) operator mean in PHP - Stack Overflow
I was diving into Symfony framework (version 4) code and found this piece of code: $env = $_SERVER['APP_ENV'] ?? 'dev'; I'm not sure what this actually does but I imagine that it expands to someth... More on stackoverflow.com
🌐 stackoverflow.com
syntax - What are the PHP operators "?" and ":" called and what do they do? - Stack Overflow
What are the ? and : operators in PHP? For example: (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) More on stackoverflow.com
🌐 stackoverflow.com
?: operator (the 'Elvis operator') in PHP - Stack Overflow
I saw this today in some PHP code: $items = $items ?: $this->_handle->result('next', $this->_result, $this); I'm not familiar with the ?: operator being used here. It looks like a ternary More on stackoverflow.com
🌐 stackoverflow.com
php 7.4 - (??=) Double question mark and an equal sign, what does that operator do? - Stack Overflow
Once I stumbled with php7 code with operator ??=. I tried to search, what it clearly does, but could not find easily. I tried to read out the php operators and even most official resources have all More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/php › understanding the use of the "..." operator in php
r/PHP on Reddit: Understanding the Use of the "..." Operator in PHP
September 20, 2023 -

I recently came across a PHP code snippet that uses the "..." operator in a context that I'm not very familiar with. The code includes a method called initials that uses the "..." operator in an array_map function. Here's the code snippet for reference:

class HighSchoolSweetheart
{
    public function initial(string $name): string
    {
        return strtoupper($this->firstLetter($name)) . '.';
    }
    
    public function initials(string $name): string
    {
        $initials = array_map($this->initial(...), explode(' ', $name));
    
        return implode(' ', $initials);
    }
}

I understand that the "..." operator is often used for argument unpacking in PHP and that array_map is used to apply a callback function ($this->initial(...)) to each element of an array created by explode(' ', $name). However, I'm not entirely clear on how the splat operator "..." functions in this context. Can someone explain its role in this code and how it affects the initial method? Thanks!

Top answer
1 of 3
668

It's the "null coalescing operator", added in php 7.0. The definition of how it works is:

It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.

So it's actually just isset() in a handy operator.

Those two are equivalent1:

bar ?? 'something';
$foo = isset(bar : 'something';

Documentation: http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.coalesce

In the list of new PHP7 features: http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op

And original RFC https://wiki.php.net/rfc/isset_ternary


EDIT: As this answer gets a lot of views, little clarification:

1There is a difference: In case of ??, the first expression is evaluated only once, as opposed to ? :, where the expression is first evaluated in the condition section, then the second time in the "answer" section.

2 of 3
55
$myVar = $someVar ?? 42;

Is equivalent to :

$myVar = isset($someVar) ? $someVar : 42;

For constants, the behaviour is the same when using a constant that already exists :

define("FOO", "bar");
define("BAR", null);

$MyVar = FOO ?? "42";
$MyVar2 = BAR ?? "42";

echo $MyVar . PHP_EOL;  // bar
echo $MyVar2 . PHP_EOL; // 42

However, for constants that don't exist, this is different :

$MyVar3 = IDONTEXIST ?? "42"; // Raises a warning
echo $MyVar3 . PHP_EOL;       // IDONTEXIST

Warning: Use of undefined constant IDONTEXIST - assumed 'IDONTEXIST' (this will throw an Error in a future version of PHP)

Php will convert the non-existing constant to a string.

You can use constant("ConstantName") that returns the value of the constant or null if the constant doesn't exist, but it will still raise a warning. You can prepended the function with the error control operator @ to ignore the warning message :

$myVar = @constant("IDONTEXIST") ?? "42"; // No warning displayed anymore
echo $myVar . PHP_EOL; // 42
Top answer
1 of 10
87

This is the conditional operator.

y : $z

means "if $x is true, then use $y; otherwise use $z".

It also has a short form.

z

means "if $x is true, then use $x; otherwise use $z".

People will tell you that ?: is "the ternary operator". This is wrong. ?: is a ternary operator, which means that it has three operands. People wind up thinking its name is "the ternary operator" because it's often the only ternary operator a given language has.

2 of 10
33

I'm going to write a little bit on ternaries, what they are, how to use them, when and why to use them and when not to use them.

What is a ternary operator?

A ternary ? : is shorthand for if and else. That's basically it. See "Ternary Operators" half way down this page for more of an official explanation.

As of PHP 5.3:

Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

As of PHP 7.0

PHP 7 has new Null Coalesce Operator. This is the same as a ternary but is also called an "isset ternary". This also allows a set of chained ternaries that remove the need for isset() checks.

In PHP 5, if you wanted to use a ternary with a potentially non-existent variable then you would have to perform an isset() at the beginning of the ternary statement:

$result = isset($nonExistentVariable) ? $nonExistentVariable : ‘default’;

In PHP 7, you can now do this instead:

$result = $nonExistentVariable ?? ‘default’;

The Null Coalesce Operator does not work with an empty string, however, so bear that in mind. The great thing about this is you can also chain the operators for multiple checks for multiple variables, providing a sort of backup depending on whether or not each variable in the chain exists:

$user = $userImpersonatingAnotherUser ?? $loggedInUser ?? “Guest”;

In PHP, with systems where a user can login, it is not uncommon for an administrator to be able to impersonate another user for testing purposes. With the above example, if the user is not impersonating another user, and also a logged in user does not exist, then the user will be a guest user instead. Read on more if you don't understand this yet to see what ternaries are and how they are used, and then come back to this bit to see how the new PHP

How are ternaries used?

Here's how a normal if statement looks:

if (isset($_POST['hello']))
{
    $var = 'exists';
}
else
{
    $var = 'error';
}

Let's shorten that down into a ternary.

$var = isset($_POST['hello']) ? 'exists' : 'error';
                 ^            ^     ^    ^     |
                 |           then   |   else   |
                 |                  |          |
          if post isset         $var=this   $var=this

Much shorter, but maybe harder to read. Not only are they used for setting variables like $var in the previous example, but you can also do this with echo, and to check if a variable is false or not:

$isWinner = false;

// Outputs 'you lose'
echo ($isWinner) ? 'You win!' : 'You lose';

// Same goes for return
return ($isWinner) ? 'You win!' : 'You lose';

Why do people use them?

I think ternaries are sexy. Some developers like to show off, but sometimes ternaries just look nice in your code, especially when combined with other features like PHP 5.4's latest short echos.

<?php 
    $array = array(0 => 'orange', 1 => 'multicoloured'); 
?>

<div>
    <?php foreach ($array as value) { ?>
        <span><?=($value==='multicoloured')?'nonsense':'pointless'?></span>
    <?php } ?>
</div>

<!-- Outputs:
    <span>
        pointless
    </span>
    <span>
        nonsense
    </span> 
-->

Going off-topic slightly, when you're in a 'view/template' (if you're seperating your concerns through the MVC paradigm), you want as little server-side logic in there as possible. So, using ternaries and other short-hand code is sometimes the best way forward. By "other short-hand code", I mean:

if ($isWinner) :
    // Show something cool
endif;

Note, I personally do not like this kind of shorthand if / endif nonsense

How fast is the ternary operator?

People LIKE micro-optimisations. They just do. So for some, it's important to know how much faster things like ternaries are when compared with normal if / else statements.

Reading this post, the differences are about 0.5ms. That's a lot!

Oh wait, no it's not. It's only a lot if you're doing thousands upon thousands of them in a row, repeatedly. Which you won't be. So don't worry about speed optimisation at all, it's absolutely pointless here.

When not to use ternaries

Your code should be:

  • Easy to read
  • Easy to understand
  • Easy to modify

Obviously this is subject to the persons intelligence and coding knowledge / general level of understanding on such concepts when coming to look at your code. A single simple ternary like the previous examples are okay, something like the following, however, is not what you should be doing:

echo ($colour === 'red') ? "Omg we're going to die" :
     ($colour === 'blue' ? "Ah sunshine and daisies" :
     ($colour === 'green' ? "Trees are green"
     : "The bloody colour is orange, isn't it? That was pointless."));

That was pointless for three reasons:

  • Ridiculously long ternary embedding
  • Could've just used a switch statement
  • It was orange in the first place

Conclusion

Ternaries really are simple and nothing to get too worked up about. Don't consider any speed improvements, it really won't make a difference. Use them when they are simple and look nice, and always make sure your code will be readable by others in the future. If that means no ternaries, then don't use ternaries.

🌐
GeeksforGeeks
geeksforgeeks.org › php › php-operators
PHP Operators - GeeksforGeeks
June 14, 2025 - In PHP, operators are special symbols used to perform operations on variables and values. Operators help you perform a variety of tasks, such as mathematical calculations, string manipulations, logical comparisons, and more.
Find elsewhere
🌐
Uptimia
uptimia.com › home › questions › what does the ?? operator mean in php?
What Does The ?? Operator Mean In PHP?
October 31, 2024 - The ?? operator, or null coalescing operator, was added in PHP 7.0. It returns the first non-null value in a series of expressions.
🌐
PHP.Watch
php.watch › versions › 8.5 › pipe-operator
Pipe operator (`|>`) - PHP 8.5 • PHP.Watch
PHP 8.5 adds a new operator, the pipe operator (|>) to chain multiple callables from left to right, taking the return value of the left callable and passing it to the right.
🌐
Exakat
exakat.io › home › weird operators in php
Weird operators in PHP - Exakat
October 6, 2020 - Operators are usually made up with strange symbols, like !, -, =>, <=>, ^ or ~. Really, some are plain readable like and, while some are merely an missed attempt at being readable, and actually hide a double personnality, like xor. You probably think you know PHP’s documentation in and out, but there is always more to learn.
🌐
IONOS
ionos.com › digital guide › websites › web development › php operators
What are PHP operators in programming? - IONOS
October 18, 2024 - PHP operators are special symbols or strings for operations on PHP variables and values. They’re used to manipulate data, check conditions, and perform mathematical operations. They are often included in PHP functions or PHP classes.
🌐
Stitcher
stitcher.io › blog › shorthand-comparisons-in-php
Shorthand comparisons in PHP | Stitcher.io
Interesting fact: the name ternary operator actually means "an operator which acts on three operands". An operand is the term used to denote the parts needed by an expression. The ternary operator is the only operator in PHP which requires three operands: the condition, the true and the false result.