How to write a PHP ternary operator
Solution 1
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")));
Solution 2
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);
Solution 3
echo ($result ->vocation == 1) ? 'Sorcerer'
: ($result->vocation == 2) ? 'Druid'
: ($result->vocation == 3) ? 'Paladin'
....
;
It’s kind of ugly. You should stick with normal if
statements.
Solution 4
How to write a basic PHP Ternary Operator:
($your_boolean) ? 'This is returned if true' : 'This is returned if false';
Example:
$myboolean = true;
echo ($myboolean) ? 'foobar' : "penguin";
foobar
echo (!$myboolean) ? 'foobar' : "penguin";
penguin
A PHP ternary operator with an 'elseif' crammed in there:
$chow = 3;
echo ($chow == 1) ? "one" : ($chow == 2) ? "two" : "three";
three
But please don't nest ternary operators except for parlor tricks. It's a bad code smell.
Solution 5
I'd rather than ternary if-statements go with a switch-case. For example:
switch($result->vocation){
case 1:
echo "Sorcerer";
break;
case 2:
echo "Druid";
break;
case 3:
echo "Paladin";
break;
case 4:
echo "Knight";
break;
case 5:
echo "Master Sorcerer";
break;
case 6:
echo "Elder Druid";
break;
case 7:
echo "Royal Paladin";
break;
default:
echo "Elite Knight";
break;
}
dynamitem
Updated on July 05, 2022Comments
-
dynamitem almost 2 years
How do I write a PHP ternary operator with the elseif portion?
I see basic examples with the
if
andelse
portions of the PHP ternary operator like this:echo (true) ? "yes" : "no"; //prints yes echo (false) ? "yes" : "no"; //prints no
How do I get the "elseif" portion like this into the ternary operator?
<?php if($result->vocation == 1){ echo "Sorcerer"; }else if($result->vocation == 2){ echo 'Druid'; }else if($result->vocation == 3){ echo 'Paladin'; }else if($result->vocation == 4){ echo 'Knight'; }else if($result->vocation == 5){ echo 'Master Sorcerer'; }else if($result->vocation == 6){ echo 'Elder Druid'; }else if($result->vocation == 7){ echo 'Royal Paladin'; }else{ echo 'Elite Knight'; } ?>
-
mpen about 10 yearsThe
break
s are redundant when you're usingreturn
. -
offchance over 8 yearsCan you please remove that space before first arrow? Can't edit less than 6 characters.
-
AhmadKarim almost 4 yearsInstead of doing this
$qvar = isset($_GET['qvar']) ? $_GET['qvar'] : 'default';
we can do this$qvar = $_GET['qvar'] ?? 'default';
I am not sure which PHP version was this introduced in? -
compuphys over 3 years@AhmadKarim this is called the null coalescing operator (??) and was introduced in PHP 7.