How to write a PHP ternary operator

PhpTernary Operator

Php Problem Overview


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
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';
  }
?>

Php Solutions


Solution 1 - Php

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 - Php

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 - Php

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 - Php

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 - Php

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;
}

Solution 6 - Php

You wouldn’t: it’s messy and hard to read.

You’re looking for the switch statement in the first case. The second is fine as it is but still could be converted for consistency

Ternary statements are much more suited to boolean values and alternating logic.

Solution 7 - Php

To be honest, a ternary operator would only make this worse, what i would suggest if making it simpler is what you are aiming at is:

$groups = array(1=>"Player", 2=>"Gamemaster", 3=>"God");
echo($groups[$result->group_id]);

and then a similar one for your vocations

$vocations = array(
  1=>"Sorcerer",
  2=>"Druid",
  3=>"Paladin",
  4=>"Knight",
  ....
);
echo($vocations[$result->vocation]);

With a ternary operator, you would end up with

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

Which as you can tell, only gets more complicated the more you add to it

Solution 8 - Php

In addition to all the other answers, you could use switch. But it does seem a bit long.

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;
}

Solution 9 - Php

You could also do:

echo "yes" ?: "no" // Assuming that yes is a variable that can be false.

Instead of:

echo (true)  ? "yes" : "no";

Solution 10 - Php

PHP 8 (Left-associative ternary operator change)

Left-associative ternary operator deprecation https://wiki.php.net/rfc/ternary_associativity. The ternary operator has some weird quirks in PHP. This RFC adds a deprecation warning for nested ternary statements. In PHP 8, this deprecation will be converted to a compile time error.

1 ? 2 : 3 ? 4 : 5;   // deprecated
(1 ? 2 : 3) ? 4 : 5; // ok

source: https://stitcher.io/blog/new-in-php-74#numeric-literal-separator-rfc

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondynamitemView Question on Stackoverflow
Solution 1 - Phprandom_user_nameView Answer on Stackoverflow
Solution 2 - PhpOrangepillView Answer on Stackoverflow
Solution 3 - PhpthelolcatView Answer on Stackoverflow
Solution 4 - PhpEric LeschinskiView Answer on Stackoverflow
Solution 5 - PhpmadsroskarView Answer on Stackoverflow
Solution 6 - PhpexussumView Answer on Stackoverflow
Solution 7 - PhpbizzehdeeView Answer on Stackoverflow
Solution 8 - Phpfederico-tView Answer on Stackoverflow
Solution 9 - PhpJann Anthony BrizaView Answer on Stackoverflow
Solution 10 - PhpDevWLView Answer on Stackoverflow