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
🌐
FlatCoding
flatcoding.com β€Ί home β€Ί php elvis operator: how (?:) works with examples
PHP Elvis Operator: How (?:) Works with Examples - FlatCoding
June 29, 2025 - The Elvis operator in PHP is a shorthand version of the ternary operator. You write it as ?:. It checks if a value is true. If yes, it returns that value. If not, it returns the fallback.
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
🌐
Web Tips
webtipblog.com β€Ί home β€Ί php β€Ί php and the elvis operator
PHP and the Elvis Operator - Web Tips
April 5, 2014 - The Elvis operator has been available since PHP 5.3 so check your version before using on your site. According to php.net, β€œSince PHP 5.3, it is possible to leave out the middle part of the ternary operator.
🌐
W3Docs
w3docs.com β€Ί php
?: operator (the 'Elvis operator') in PHP
It is often referred to as the Elvis operator because it looks like a stylized pair of eyes and the top of a head, similar to the hairstyle of Elvis Presley. ... <?php $age = 25; // Using the ternary operator to determine if a person is an adult ...
🌐
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.
🌐
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 ...
🌐
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
🌐
Wikipedia
en.wikipedia.org β€Ί wiki β€Ί Elvis_operator
Elvis operator - Wikipedia
2 weeks ago - In PHP, it is possible to leave out the middle part of the ternary operator since PHP 5.3. (June 2009). The Fantom programming language has the ?: binary operator that compares its first operand with null.
🌐
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 - The null coalescing operator (??) was added in PHP 7.0. It returns the first argument if it's set and not null. If the first argument is null or not set, it returns the second argument.
🌐
Fernando Paredes
fparedes.com β€Ί blog β€Ί null coalescing (??) operator vs elvis (?:) operator in php – they are different!
Null coalescing (??) operator vs Elvis (?:) operator in PHP – they are different!
November 20, 2018 - php > var_dump( false ?: 'foo' ); string(7) "foo" php > var_dump( false ?? 'bar' ); bool(false) The Elvis operator was introduced in PHP 5.3.
🌐
CodeNewbie
community.codenewbie.org β€Ί montasser β€Ί the-php-elvis-operator-explained-3bpm
The PHP Elvis Operator (?:) Explained - CodeNewbie Community 🌱
May 9, 2025 - The Elvis operator in PHP looks like this: ?:. It offers a short way to check a value and fall back to another one if the first is not usable. PHP introduces it in version 5.3.
🌐
Web Developer
webdeveloper.com β€Ί tips-tricks β€Ί elvis-operator-in-php-a-complete-guide
Elvis Operator in PHP: A Complete Guide
Just be mindful of its behavior with different types of falsey values to avoid unintended outcomes. ... about: ({ version: 0.1.9 β€” BETA 12.3, social: @webDeveloperHQ, }); legal: ({ terms: of use, privacy: policy analytics: Fullres });
🌐
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 ...
🌐
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:
🌐
Nulldog
nulldog.com β€Ί php-elvis-vs-null-coalescing-which-to-use
PHP Elvis vs Null Coalescing: Which to Use?
PHP Version Compatibility: The null coalescing operator (??) was introduced in PHP 7.0, so it's crucial to ensure compatibility if your code needs to run on older PHP versions. The Elvis operator (?:), however, has been available since PHP 5.3.
🌐
Itsourcecode
itsourcecode.com β€Ί home β€Ί php elvis operator (detailed explanation with examples)
PHP Elvis Operator (Detailed Explanation With Examples)
November 22, 2023 - If the operand is valid, the first operand is returned; otherwise, the second operand is evaluated and returned. ... In PHP, the Elvis operator returns a non-null value even if the conditional expression is null.
🌐
WordPress Trac
core.trac.wordpress.org β€Ί ticket β€Ί 58948
#58948 (Use Elvis operator (`:?`) to replace repeating ternary patterns (`a ? a : b`)) – WordPress Trac
Replaces a ? a : b patterns of ternary statements to use the Elvis operator a ?: b. Null coalescing operator (??) was added in PHP 7.0, which WordPress now requires as the minimum version.