How to write a PHP ternary operator

dynamitem

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

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")));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to write following code with Conditional(Ternary) Operator?

From Dev

How to use continue keyword in a ternary operator in php

From Dev

How to use php ternary operator in the parameter of a function?

From Dev

How to use continue keyword in a ternary operator in php

From Dev

understading the ternary operator in php

From Dev

evaluation of ternary operator in php

From Dev

Ternary Operator in PHP

From Dev

PHP Ternary Operator Misunderstanding?

From Dev

evaluation of ternary operator in php

From Dev

How to write if else condition using ternary operator in jstl?

From Dev

How would I write this if else statement as ternary operator

From Dev

How do I use Ternary Operator within awk to write to a file?

From Dev

How to transform in ternary operator?

From Dev

How to Convert this to a ternary operator

From Dev

How ternary operator works?

From Dev

How to use a shorthand ternary operator php in sql query ?

From Dev

PHP ternary operator syntax error

From Dev

Understanding PHP's ternary operator

From Dev

ternary operator in php with echo value

From Dev

Understanding PHP's ternary operator

From Dev

PHP ternary operator syntax error

From Dev

Conditional ternary operator malfunctions (PHP)

From Dev

Ternary operator inside calculation PHP

From Dev

Ternary operator in PHP generated table

From Java

PHP ternary operator vs null coalescing operator

From Dev

How to use Ternary Operator in JSP

From Dev

How is ternary operator implemented in Python

From Dev

How to rewrite Swift ++ operator in ?: ternary operator

From Dev

How to apply += operator inside a conditional ternary operator

Related Related

HotTag

Archive