This is only available since PHP 5.3

The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.

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.1

See this example for more context.

or a more useful but note in the comments: http://www.php.net/manual/en/control-structures.if.php#102060


1http://php.net/manual/en/language.operators.comparison.php

Answer from azat on Stack Overflow
๐ŸŒ
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.
๐ŸŒ
Stitcher
stitcher.io โ€บ blog โ€บ shorthand-comparisons-in-php
Shorthand comparisons in PHP | Stitcher.io
I believe the right thing to do is to avoid nested ternary operators altogether. You can read more about this strange behaviour in this Stack Overflow answer. Furthermore, as PHP 7.4, the use of chained ternaries without brackets is deprecated.
๐ŸŒ
PHP Tutorial
phptutorial.net โ€บ home โ€บ php tutorial โ€บ php ternary operator
PHP Ternary Operator
April 6, 2025 - Note that the name ternary operator comes from the fact that this operator requires three operands: expression, value1, value2. Suppose you want to display the login link if the user has not logged in and the logout link if the user has already logged in. To do that, you can use the if...else statement as follows: <?php $is_user_logged_in = false; if ($is_user_logged_in) { $title = 'Logout'; } else { $title = 'Login'; } echo $title;Code language: HTML, XML (xml)
๐ŸŒ
PHP.Watch
php.watch โ€บ articles โ€บ php-ternary-and-coalescing
Ternary and Ternary Coalescing Operator in PHP โ€ข PHP.Watch
May 27, 2020 - Null Coalescing operator can be be further reduced in PHP 7.4, with Null Coalescing Assignment operator . ... Be mindful when you chain ternary/coalescing operators.
๐ŸŒ
PHP
wiki.php.net โ€บ rfc โ€บ ternary_associativity
PHP RFC: Deprecate left-associative ternary operator
Code exploiting left-associativity of the ternary operator will become a hard error in PHP 8.
๐ŸŒ
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 - It is called a ternary operator because it takes three operands โ€“ a condition, a result for true, and a result for false. ... Condition: It is the expression to be evaluated which returns a boolean value.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ php-ternary-operator
PHP | Ternary Operator - GeeksforGeeks
July 12, 2025 - The result of this comparison can also be assigned to a variable using the assignment operator. The syntax is as follows: Variable = (Condition) ? (Statement1) : (Statement2); If the statement executed depending on the condition returns any value, it will be assigned to the variable. Advantages of Ternary Operator: Following are some advantages of ternary operator:
๐ŸŒ
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
September 16, 2025 - 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
๐ŸŒ
Pi My Life Up
pimylifeup.com โ€บ home โ€บ using the ternary operator in php
Using the Ternary Operator in PHP - Pi My Life Up
June 29, 2022 - The ternary operator is written using a question mark (?), followed by a colon (:). These two symbols can be written separated like โ€œ? :โ€ or using the shorting hand version with both ...
๐ŸŒ
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 ...
๐ŸŒ
BrainBell
brainbell.com โ€บ php โ€บ ternary-operator.html
Ternary, Elvis, and Null Coalescing Operators in PHP โ€“ BrainBell
May 11, 2023 - The ternary operator ?: works with three sets of data: ... If the first set (condition) is true then the result is the second set (after the question mark). If the first set (condition) is false, the result is the third set (after the colon).
๐ŸŒ
Abeautifulsite
abeautifulsite.net โ€บ posts โ€บ how-to-use-the-php-ternary-operator
How to use the PHP ternary operator
This trick only works in PHP 5.3+ and can sometimes make your logic even shorter. Consider this: if ($start) { $start = $start; } else { $start = 1; } Granted, you probably wouldn't do that in your code.
๐ŸŒ
AEZ-Tech
aez-tech.site โ€บ home โ€บ php tutorials โ€บ ternary operator
Ternary Operator ( ? : ) - PHP
November 16, 2025 - <?php // ternary operator print("<p>Case 1 : </p>"); $a = False; // Declare Variable $a ? print("True") : print("False") ; print("<p> a = $a </p>"); print("<p>Case 2 : </p>"); $z = True; // Declare Variable $z ? print("True") : print("False") ; print("<p> z = $z </p>"); ?> Case 1 : False a = Case 2 : True z = 1 ยท โ€”-The PHP version used in this example is ยท
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ home โ€บ php โ€บ php null coalescing operator
PHP Null Coalescing Operator
May 26, 2007 - The Null Coalescing operator is one of the many new features introduced in PHP 7. The word "coalescing" means uniting many things into one. This operator is used to replace the ternary operation in conjunction with the isset() function.
๐ŸŒ
Envato Tuts+
code.tutsplus.com โ€บ home โ€บ coding fundamentals
Crash Course in the PHP Ternary Operator With Examples | Envato Tuts+
May 27, 2021 - The ternary operator (? and :) is a conditional operator which allows you to execute a condition, and based on the result of the condition, it allows you to return different results. In most cases, you could use it as an alternative to the if-else statement, specifically when you want to assign variables based on the outcome of the specific condition. Letโ€™s go through the syntax of the ternary operator.