DEV Community

Emmanuel Gautier
Emmanuel Gautier

Posted on • Originally published at emmanuelgautier.com on

PHP Ternary Operator

The usage of the ternary operator is not the simplest and the most readable way to develop but can be useful for simple conditions. The implementation of the ternary operator depends on the language, let's see how to do it in PHP.

What is the ternary operator?

It is a one line way to write some conditions like the following example:

if($boolean) {
  echo "foo";
} else {
  echo "bar";
}
Enter fullscreen mode Exit fullscreen mode

This condition is simple but takes 5 lines of code. The ternary operator allows to have this condition in only one line without removing too much readability here:

echo ($boolean ? "foo" : "bar");
Enter fullscreen mode Exit fullscreen mode

You write your code like this for every use case and have a ternary operator into a ternary operator. But this way can lower the readability of your code, so making the application harder to understand and to maintain:

echo ($boolean ? ($boolean2 ? "true true" : "true false") : "false");
Enter fullscreen mode Exit fullscreen mode

The ternary operator could be write as following as well:

$value = $value ?: $othervalue;
Enter fullscreen mode Exit fullscreen mode

This way could be useful to assign a default value. Let's take as an example one use case where we want to display as name, the family name if exists else the username :

$display_name = $lastname ?: $username;
Enter fullscreen mode Exit fullscreen mode

Documentation

Top comments (1)

Collapse
 
maksimepikhin profile image
Maksim N Epikhin (PIKHTA)

Php has an interesting feature of the ternary operator - a specific and unique order of execution.

$ python -c "print 1 if true else 2 if true else 3 if true else 4 if true else 5"
 1

$ node   -e "      true ? 1 : true ? 2 : true ? 3 : true ? 4 : 5"
 1

$ perl   -e "print true ? 1 : true ? 2 : true ? 3 : true ? 4 : 5"
 1

$ ruby   -e "print true ? 1 : true ? 2 : true ? 3 : true ? 4 : 5"
 1

$ php    -r "print true ? 1 : true ? 2 : true ? 3 : true ? 4 : 5;"
 4
Enter fullscreen mode Exit fullscreen mode

I have known about this interesting nuance for a long time, but just yesterday I discovered an error in one of the open sources: the author clearly did not know about this nuance and was caught. Therefore, this article is only a warning. After all, if a programmer expects the same behavior from php as from other languages, he can get into halepa.

This technique is very convenient for setting values, depending on the conditions. A neat replacement for if-else. For example:

value = isCondFirst()  ? valueFirst()  :
        isCondSecond() ? valueSecond() :
        isCondThird()  ? valueThird()  :
                         valueDefault();

/********** Instead **********/

if (isCondFirst()) {
    value = valueFirst();
} else if (isCondSecond()) {
    value = valueSecond();
} else if (isCondThird()) {
    value = valueThird();
} else {
    value = valueDefault();
}
Enter fullscreen mode Exit fullscreen mode

in the official manual: $a = true ? 0 : true ? 1 : 2; // (true ? 0 : true) ? 1 : 2 = 2