🌐
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)
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-ternary-operator
PHP | Ternary Operator - GeeksforGeeks
July 12, 2025 - It is called a ternary operator because it takes three operands- a condition, a result statement for true, and a result statement for false.
Discussions

How to write a PHP ternary operator with the elseif portion? - Stack Overflow
How do I write a PHP ternary operator with the elseif portion? I see basic examples with the if and else portions of the PHP ternary operator like this: echo (true) ? "yes" : "no"; //prints yes More on stackoverflow.com
🌐 stackoverflow.com
How do I concatenate this into 1 ternary operator?
Were I forced to put this into one ternary operator: echo in_array($shape[0], ['EC', 'RC', 'AS']) ? 'Sharp Corner' : $code[0]; or echo preg_match('/^(EC|RC|AS)$/', $shape[0]) ? 'Sharp Corner' : $code[0]; More on reddit.com
🌐 r/PHPhelp
26
2
January 6, 2021
I think the ternary operator would be more readable if the order was reversed
Python does something similar: a = b if condition else c But I feel like it's less readable. A lot of people hate the ternary operator because they think it's not very readable, but I'm quite fond of it. If things get long I type it like: a = Condition ? b : c Which I find to be perfectly readable. Oh well. More on reddit.com
🌐 r/ProgrammingLanguages
57
0
June 20, 2024
Ternary operators are the worst..unless you indent them right!
Ternarys are amazing if you don't nest them. Just make a variable for the contents or use an if statement. More on reddit.com
🌐 r/ProgrammerHumor
328
3215
September 4, 2022
🌐
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.
🌐
Stitcher
stitcher.io › blog › shorthand-comparisons-in-php
Shorthand comparisons in PHP | Stitcher.io
Since PHP 5.3, it's possible to leave out the lefthand operand, allowing for even shorter expressions: ... In this case, the value of $result will be the value of $initial, unless $initial evaluates to false, in which case the string 'default' is used. You could write this expression the same way using the normal ternary operator:
🌐
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.
🌐
PHP.Watch
php.watch › articles › php-ternary-and-coalescing
Ternary and Ternary Coalescing Operator in PHP • PHP.Watch
May 27, 2020 - Ternary operator is a short form for an if/else block that executes exactly one expression each.
🌐
W3Schools
w3schools.com › c › c_conditions_short_hand.php
C Short Hand If ... Else (Ternary Operator)
C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Compiler C Syllabus C Study Plan C Interview Q&A C Certificate ... There is also a short-hand if...else, known as the ternary operator because it uses three operands.
Find elsewhere
🌐
Envato Tuts+
code.tutsplus.com › home › coding fundamentals
Crash Course in the PHP Ternary Operator With Examples | Envato Tuts+
May 27, 2021 - Let’s quickly go through the following example to see how the ternary shorthand operator works. In the above example, if the $x variable evaluates to TRUE, the value of the $x variable is assigned to the $output variable, otherwise $output will be set to 0. I’ve also demonstrated how it would look with the regular ternary operator.
🌐
LinkedIn
linkedin.com › pulse › simplifying-if-else-php-ternary-null-safe-operators-md-enamul-haque-vgqqc
Simplifying if-else in PHP with Ternary and Null Safe Operators
April 1, 2024 - The ternary operator ? : is a condensed form of the if-else statement in PHP, allowing you to execute different expressions based on a condition in a single line.
🌐
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. You'd probably do something like this instead: ... But that's still three lines of code. Let's try with the shorthand ternary operator now:
🌐
SitePoint
sitepoint.com › blog › php › using the php ternary operator
Using the PHP Ternary Operator
February 12, 2024 - Consisting of three parts, the ternary operator uses three expressions separated by a question mark and a colon. The question mark follows the test expression and can be thought of as asking, “Well, is it true?” The colon then separates your two possible values, the first of which will ...
🌐
W3Schools
w3schools.com › php › php_if_shorthand.asp
PHP if Shorthand
zip_close() zip_entry_close() zip_entry_compressedsize() zip_entry_compressionmethod() zip_entry_filesize() zip_entry_name() zip_entry_open() zip_entry_read() zip_open() zip_read() PHP Timezones · ❮ Previous Next ❯ · To write shorter code, you can write if statements on one line. One-line if statement: $a = 5; if ($a < 10) $b = "Hello"; echo $b Try it Yourself » · if...else statements can also be written in one line, but the syntax is a bit different. One-line if...else statement: $a = 13; $b = $a < 10 ? "Hello" : "Good Bye"; echo $b; Try it Yourself » · This technique is known as Ternary Operators, or Conditional Expressions.
Top answer
1 of 10
132

A Ternary is not a good solution for what you want. It will not be readable in your code, and there are much better solutions available.

Why not use an array lookup "map" or "dictionary", like so:

$vocations = array(
    1 => "Sorcerer",
    2 => "Druid",
    3 => "Paladin",
    ...
);

echo $vocations[$result->vocation];

A ternary for this application would end up looking like this:

echo($result->group_id == 1 ? "Player" : ($result->group_id == 2 ? "Gamemaster" : ($result->group_id == 3 ? "God" : "unknown")));

Why is this bad? Because - as a single long line, you would get no valid debugging information if something were to go wrong here, the length makes it difficult to read, plus the nesting of the multiple ternaries just feels odd.

A Standard Ternary is simple, easy to read, and would look like this:

$value = ($condition) ? 'Truthy Value' : 'Falsey Value';

or

echo ($some_condition) ? 'The condition is true!' : 'The condition is false.';

A ternary is really just a convenient / shorter way to write a simple if else statement. The above sample ternary is the same as:

if ($some_condition) {
    echo 'The condition is true!';
} else {
    echo 'The condition is false!';
}

However, a ternary for a complex logic quickly becomes unreadable, and is no longer worth the brevity.

echo($result->group_id == 1 ? "Player" : ($result->group_id == 2 ? "Gamemaster" : ($result->group_id == 3 ? "God" : "unknown")));

Even with some attentive formatting to spread it over multiple lines, it's not very clear:

echo($result->group_id == 1 
    ? "Player" 
    : ($result->group_id == 2 
        ? "Gamemaster" 
        : ($result->group_id == 3 
            ? "God" 
            : "unknown")));
2 of 10
12

Since this would be a common task I would suggest wrapping a switch/case inside of a function call.

function getVocationName($vocation){
    switch($vocation){
        case 1: return "Sorcerer";
        case 2: return 'Druid';
        case 3: return 'Paladin';
        case 4: return 'Knight';
        case 5: return 'Master Sorcerer';
        case 6: return 'Elder Druid';
        case 7: return 'Royal Paladin';
        default: return 'Elite Knight';
    }
}

echo getVocationName($result->vocation);
🌐
Reddit
reddit.com › r/phphelp › how do i concatenate this into 1 ternary operator?
r/PHPhelp on Reddit: How do I concatenate this into 1 ternary operator?
January 6, 2021 -
echo $shape[0] == 'EC' ? 'Sharp Corner' : $code[0];
echo $shape[0] == 'RC' ? 'Sharp Corner' : $code[0]; 
echo $shape[0] == 'AS' ? 'Sharp Corner' : $code[0];

I want to combine all 3 ('EC' , 'RC' and 'AS') under Sharp Corner.

I tried this:

echo $shape[0] == 'EC' && 'RC' && 'AS' ? 'Sharp Corner' : $code[0];

But the server still seems to separate them for some reason.

🌐
FlatCoding
flatcoding.com › home › php ternary operator: how to write short expressions
PHP Ternary Operator: How to Write Short Expressions - FlatCoding
July 1, 2025 - PHP checks the condition. It returns one value if the condition has a true result. It returns another value if the result is false and avoids multiple lines for if-else. ... This assigns ‘Adult’ if $age is 18 or more. It assigns ‘Minor’ if not. Use the ternary operator to make simple choices.
🌐
Matt Stauffer
mattstauffer.com › blog › even-shorter-ternary-operators-in-php-using
Even shorter ternary operators in PHP using ?: | MattStauffer.com
September 3, 2013 - The syntax for the regular ternary operator is (expression) ? value if truthy : value if falsy. The expression can also just be a single variable, which will test whether the variable is truthy or falsy: <?php $output = $value ?
🌐
DevGenius
blog.devgenius.io › php-7-x-p27-ternary-operator-a625bb7fa311
PHP — P27: Ternary Operator
October 22, 2023 - The ternary operator follows the following syntax: ... If the boolean expression evaluates to true, the “if_true” result is displayed, otherwise the “if_false” result is displayed. View this article and others on my website. ... <?php$expression = true;if ( $expression ) { // do statement if true } else { // do statement if false }?>
🌐
Coderwall
coderwall.com › p › qo4i-q › stacking-the-ternary-operator-in-php
Stacking the Ternary Operator in PHP (Example)
October 27, 2018 - In PHP the ternary operator is left-associative and therefore behaves entirely incorrectly: