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")));
Answer from random_user_name on Stack Overflow
🌐
W3Schools
w3schools.in › php › operators › ternary-operator
PHP Ternary Operator - W3Schools
Condition statement: This is a valid PHP expression that will be evaluated in order to return a Boolean value. Statement_1: This will be the statement that will be executed when the conditional results will return true or be in a true state. Statement_2: This will be the statement that will be executed when the conditional results will return true or be in a false state. ... You can use the ternary operator when there is a need to simplify if-else statements or if the programmer wants to make efficient code out of a complex program structure.
🌐
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:
🌐
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
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.
🌐
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.
🌐
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);
🌐
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
Find elsewhere
🌐
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:
🌐
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
🌐
Hacking with PHP
hackingwithphp.com › 3 › 12 › 4 › the-ternary-operator
The Ternary Operator – Hacking with PHP - Practical PHP
<?php if ($age < 16) { $agestr = 'child'; } else { $agestr = 'adult'; } ?> So, in essence, using the ternary operator allows you to compact five lines of code into one, at the expense of some readability.
🌐
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 ...
🌐
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:
🌐
PixemWeb
pixemweb.com › home › php › php ternary operator explained
PHP Ternary Operator Explained - PixemWeb
February 5, 2021 - The PHP Ternary Operator is used to evaluate conditions in a more compact way vs the if/else statement. Learn how to use the ternary operator.
🌐
Phpschools
phpschools.com › lesson › php-ternary-operator
php ternary operator - phpschools
December 12, 2025 - The PHP ternary operator is a compact and efficient way to write conditional expressions in PHP. Instead of using long and repetitive if/else statements, the PHP ternary operator allows developers to evaluate a condition and return a value in a single, streamlined line of code.
🌐
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.
🌐
Tutorialspoint
tutorialspoint.com › php › php_conditional_operator_examples.htm
PHP - Conditional Operators Examples
<?php $a = 10; $b = 20; /* If condition is true then assign a to result otherwise b */ $result = ($a > $b ) ? $a :$b; echo "TEST1 : Value of result is $result \n"; /* If condition is true then assign a to result otherwise b */ $result = ($a < $b ) ? $a :$b; echo "TEST2 : Value of result is $result"; ?> ... Here we will use the conditional operator to check the even and odd numbers.
🌐
W3Schools
w3schools.invisionzone.com › server scripting › php
Ternary Operator In Mamp - PHP - W3Schools Forum
July 5, 2009 - so im using MAMP, and i am trying to get this ternary operator to work. when i use a regular if statement, my variable gets set, but if i use the ternary operator, it doesnt. I have no clue what im doing wrong. Ive looked at several tutorials and i thought i was doing this right.if statement if($...
🌐
FlatCoding
flatcoding.com › home › php ternary operator: how to write short expressions
PHP Ternary Operator: How to Write Short Expressions - FlatCoding
July 1, 2025 - You can write nested ternary operators. Use them to choose from more than two options. Read them carefully. Break them into clear levels. Here’s the breakdown step-by-step. PHP checks the first condition. If true, it returns the first value. If false, it evaluates the next ternary.