It evaluates to the left operand if the left operand is truthy, and the right operand otherwise.

In pseudocode,

foo = bar ?: baz;

roughly resolves to

foo = bar ? bar : baz;

or

if (bar) {
    foo = bar;
} else {
    foo = baz;
}

with the difference that bar will only be evaluated once.

You can also use this to do a "self-check" of foo as demonstrated in the code example you posted:

foo = foo ?: bar;

This will assign bar to foo if foo is null or falsey, else it will leave foo unchanged.

Some more examples:

<?php
    var_dump(5 ?: 0); // 5
    var_dump(false ?: 0); // 0
    var_dump(null ?: 'foo'); // 'foo'
    var_dump(true ?: 123); // true
    var_dump('rock' ?: 'roll'); // 'rock'
    var_dump('' ?: 'roll'); //  'roll'
    var_dump('0' ?: 'roll'); //  'roll'
    var_dump('42' ?: 'roll'); //  '42'
?>

By the way, it's called the Elvis operator.

Answer from BalusC on Stack Overflow
🌐
Josh
josh.fail › 2022 › phps-null-coalescing-operator-vs-elvis-operator
PHP’s null coalescing operator vs elvis operator — josh.fail
July 15, 2022 - I saw this comparison of PHP’s null coalescing operator (??) vs its elvis operator (?:).
Discussions

Using ternary and null coalescing operators in PHP
I would prefer using a loop because it makes it clearer what is happening. The long list of nested operators might be hard to scroll through: foreach(["key1", "key2", "key3"] as $key) { $cityName = $cityName ?: ($addressArray[$key] ?? null); } This makes it clear that you are just going through a bunch of possible sources of the city. More on reddit.com
🌐 r/PHP
30
18
July 17, 2021
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
People also ask

How is the Elvis operator different from the ternary operator in PHP?
The ternary operator uses three parts: a condition, a result if true, and a result if false. You must write all of them:
$value = $x ? $x : 'default';
The Elvis operator skips the middle part and works like this:
$value = $x ?: 'default';
It only works if the left side is both the condition and the result.
🌐
flatcoding.com
flatcoding.com › home › php elvis operator: how (?:) works with examples
PHP Elvis Operator: How (?:) Works with Examples - FlatCoding
Can I use the Elvis operator with function returns?
Yes. You can use it when a function might return a falsy value. For example:
$name = getUsername($id) ?: 'Guest';
If the function returns '' or null, it switches to 'Guest'.
🌐
flatcoding.com
flatcoding.com › home › php elvis operator: how (?:) works with examples
PHP Elvis Operator: How (?:) Works with Examples - FlatCoding
What is the difference between Elvis operator and null coalescing operator in PHP?
The Elvis operator checks for truthy values. That includes empty strings, 0, false, and null. If the left value is false, it returns the fallback. The null coalescing operator (??) only checks if the value is null. It returns everything else, even if the value is false, 0, or an empty string.
🌐
flatcoding.com
flatcoding.com › home › php elvis operator: how (?:) works with examples
PHP Elvis Operator: How (?:) Works with Examples - FlatCoding
🌐
Shkodenko Taras
shkodenko.com › home › php › php ?: ternary operator the elvis operator
PHP ?: ternary operator the Elvis operator | Shkodenko Taras
May 16, 2022 - In PHP 5.3, a shorter syntax for the ternary operator was introduced, which allows to leave out the middle part of the ternary operator for a quick shorthand evaluation. It is also called the Elvis operator sometimes because:
🌐
FlatCoding
flatcoding.com › home › php elvis operator: how (?:) works with examples
PHP Elvis Operator: How (?:) Works with Examples - FlatCoding
June 29, 2025 - The PHP Elvis operator (?:) returns a default value if the first is falsy. Click here to see how it works in PHP with examples.
🌐
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 ...
🌐
PHP
php.net › manual › en › language.operators.comparison.php
PHP: Comparison - Manual
Between the "shortcut ternary" (aka "elvis") and "spaceship" operators, you can write some quite compact comparison functions for usort and its ilk. If you want to sort an array of associative arrays by several different keys you can chain them ...
Find elsewhere
🌐
Laravel-hub
laravel-hub.com › blog › understanding-the-elvis-operator-vs-the-null-coalescing-operator-in-php-the-clear-simple-difference
Understanding ?: vs ?? in PHP: The Clear, Simple Difference
November 26, 2025 - Laravel Hub is an all-in-one platform and community for Laravel developers to discover packages, stay updated with curated content, and connect with other professionals in the ecosystem.
🌐
DEV Community
dev.to › michael_para_188fc36258ec › elvis-operator-in-php-46d8
Elvis Operator in PHP - DEV Community
June 4, 2025 - The Elvis operator is a shorthand for a simple if-else expression. PHP does not have a native Elvis operator, but you can simulate it using the ternary operator without the middle part.
🌐
SitePoint
sitepoint.com › blog › php › using the php ternary operator
Using the PHP Ternary Operator
February 12, 2024 - Yes, you can nest ternary operators in PHP. This means you can have a ternary operator within another ternary operator. However, this can make your code more complex and harder to read, so it should be done with caution. The Elvis operator is a shorthand version of the ternary operator in PHP.
🌐
Wikipedia
en.wikipedia.org › wiki › Elvis_operator
Elvis operator - Wikipedia
2 weeks ago - The Fantom programming language has the ?: binary operator that compares its first operand with null. In Kotlin, the Elvis operator returns its left-hand side if it is not null, and its right-hand side otherwise.
🌐
Itsourcecode
itsourcecode.com › home › php elvis operator (detailed explanation with examples)
PHP Elvis Operator (Detailed Explanation With Examples)
November 22, 2023 - Elvis is also a binary operator that returns its first operand if the value of that first operand is true. This operator evaluates its second operand and returns its first operand if its first operand evaluates to false.
🌐
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
🌐
Simplilearn
simplilearn.com › home › resources › software development › what is ternary operator in php: syntax, advantages & more
What Is Ternary Operator in PHP: Syntax, Advantages & More | Simplilearn
2 weeks ago - Explore what is ternary operator in PHP in detail by understanding its syntax, advanatges, and parameteres. Read on to understand this conditional operator with sample code
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-ternary-operator
PHP | Ternary Operator - GeeksforGeeks
July 12, 2025 - If-else and Switch cases are used to evaluate conditions and decide the flow of a program. The ternary operator is a shortcut operator used for shortening the conditional statements.
🌐
DEV Community
dev.to › thicha0 › elvis-operator-vs-null-coalescing-operator-2l31
Elvis operator ?: vs Null coalescing operator - DEV Community
May 30, 2024 - The Elvis operator is used to return a default value when the given operand is false.
🌐
W3Docs
w3docs.com › php
?: operator (the 'Elvis operator') in PHP
<?php $age = 20; $can_drink = $age ... greater than or equal to 21. The Elvis operator can be used to simplify expressions that would otherwise require an if-else statement....
🌐
Uptimia
uptimia.com › home › questions › what's the difference between php's elvis (?:) and null coalescing (??) operators?
What's The Difference Between PHP's Elvis (?:) And Null Coalescing (??) Operators?
October 27, 2024 - Elvis operator: Returns the second argument for falsy values (0, empty string, false). Null coalescing operator: Returns the first argument if it's set, even if it's falsy. These differences make each operator useful for specific scenarios in ...
🌐
CodeBasics
code-basics.com › programming › php course › elvis operator
Elvis operator | PHP | CodeBasics
<?php function generateGreeting($name, $nickname) { $user = $name ?: $nickname; return "Hello, {$user}!"; }Expand code · ?: is a binary operator that returns the first operand if it's true, and the second if it isn't. ... Also, Elvis sounds ...