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
🌐
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....
People also ask

What is the Elvis operator in PHP?
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. It avoids repeating the same variable twice when setting defaults.
🌐
flatcoding.com
flatcoding.com › home › php elvis operator: how (?:) works with examples
PHP Elvis Operator: How (?:) Works with Examples - FlatCoding
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
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
🌐
DEV Community
dev.to › michael_para_188fc36258ec › elvis-operator-in-php-46d8
Elvis Operator in PHP - DEV Community
June 4, 2025 - Use the null coalescing operator (??) when you want to check only null. ... Choose based on what counts as "missing" in your logic. ... The Elvis pattern saves time and makes code shorter—but it needs thoughtful use. ... Learned to code in the wild west time of php 4, also the time xml and xpath where the new hot thing.
🌐
FlatCoding
flatcoding.com › home › php elvis operator: how (?:) works with examples
PHP Elvis Operator: How (?:) Works with Examples - FlatCoding
June 29, 2025 - You can chain Elvis operators to test multiple fallback values. $first = ''; $second = null; $third = 'Final Value'; $result = $first ?: $second ?: $third; echo $result; // Output: Final Value · PHP checks each value from left to right.
🌐
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 ...
🌐
W3Schools
w3schools.in › php › operators › ternary-operator
PHP Ternary Operator - W3Schools
Shorthand can also be used with this ternary operator by leaving out the ternary operator's central portion. This shorthand operator is also known as Elvis operator, which is written as:
🌐
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.
Find elsewhere
🌐
Web Tips
webtipblog.com › home › php › php and the elvis operator
PHP and the Elvis Operator - Web Tips
April 5, 2014 - In PHP the ternary operator can really help clean up your code, especially for short conditional assignments. The ternary operator can help improve the readability of your code as well. Someone recently enlightened me and showed me the Elvis operator and it’s usage for simple assignments.
🌐
w3tutorials
w3tutorials.net › blog › operator-the-elvis-operator-in-php
PHP Elvis Operator (?:) Explained: What Does the Shorthand Ternary Mean When the True Expression is Omitted? — w3tutorials.net
If you’ve ever encountered $value ?: 'default' in PHP code and wondered why the middle part is missing, you’re in the right place. This blog will demystify the Elvis operator, explain its behavior when the true expression is omitted, and show you how to use it effectively in your projects.
🌐
Wikipedia
en.wikipedia.org › wiki › Elvis_operator
Elvis operator - Wikipedia
3 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.
🌐
Web Developer
webdeveloper.com › tips-tricks › elvis-operator-in-php-a-complete-guide
Elvis Operator in PHP: A Complete Guide
It's particularly useful for providing default values. ### Basic Usage The syntax of the Elvis operator is: ```php $result = $firstValue ?: $defaultValue; ``` This is equivalent to: ```php $result = $firstValue ? $firstValue : $defaultValue; ``` However, with the Elvis operator, you don't need to repeat the first operand, making the code cleaner and more concise.
🌐
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 ...
🌐
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 ...
🌐
CodeNewbie
community.codenewbie.org › montasser › the-php-elvis-operator-explained-3bpm
The PHP Elvis Operator (?:) Explained - CodeNewbie Community 🌱
May 9, 2025 - As a tech enthusiast, I love to write informative articles about new technology trends and practices. ... Iselin, New Jersey. ... The PHP Elvis operator (?:) is a concise way to check a value and use a fallback if the first value is not usable.
🌐
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.
🌐
Shkodenko Taras
shkodenko.com › home › php › php ?: ternary operator the elvis operator
PHP ?: ternary operator the Elvis operator | Shkodenko Taras
May 16, 2022 - You can use ?: ternary operator in PHP then you need not empty value. Set value to a $action variable from $_POST[‘action’] parameter if it is not empty or use string default instead.
🌐
StackHub
stackhub-dev.pages.dev › post › php-short-ternary-elvis-operator-vs-null-coalescing-operator
PHP short-ternary Elvis operator vs null coalescing operator - StackHub
January 26, 2025 - Take the due function (Elvis ?: for each falsy values, null coalescing ?? for lone null). Supply the default worth oregon alternate logic connected the correct-manus broadside of the function. Larn much astir PHP operators. For additional speechmaking connected these operators: PHP Examination Operators · PHP Operators - W3Schools ·
🌐
Codementor
codementor.io › community › ternary operator in php | how to use the php ternary operator
Ternary Operator in PHP | How to use the PHP Ternary Operator | Codementor
July 19, 2019 - Elvis operator can be used in order to reduce redundancy of your conditions and shorten the length of your assignments. It is the ternary operator with the second operand omitted.
🌐
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 ...