How to Execute Multiple Actions in a Ternary Block - PHP

In PHP, if you use a comma (,) to separate your actions, you'll run into a syntax error. So the best approach is to use brackets () to group your ...


Simon Ugorji

a year ago | 3 min read

I've written quite a few tutorials on Ternary operators.

My last post was about executing multiple actions in a Ternary block in JavaScript. Today, we will focus on PHP.


Consider the code below

$a = null; $b = null; $c = null;
(!$a && !$b && !$c) ?
($a = 1).
($b = 2).
($c = 3).
(print("A, B, C = ".$a.' '.$b.' '.$c))
: (print("A, B AND C are not empty!"));

About the Snippet above

  • Using a ternary operator, we are able to check if the variables are empty (!$a && !$b && !$c).
  • Now if the variables are empty, we will reassign values to each of them and print out their current values.
  • But if the variables are not empty, we will print "A, B AND C are not empty".

The Logic

In my previous post, I explained that if you want to execute multiple actions in a ternary block in JavaScript, you must use a comma ( , ) to separate the actions but in PHP, it is quite different.

In PHP, if you use a comma ( , ) to separate your actions, you'll run into a syntax error.

So the best approach is to use brackets () to group your individual actions, then use a period ( . ) to separate them.

However, I think that the ternary operator isn't well implemented in PHP or maybe there's another way to handle it or maybe it's because of the PHP version that I run.

Here is the list of things that can bug you, when working with Ternary operators in PHP.

  • You can't use the echo statement within ternary blocks.

$f = 0;
($f == 0) ?
echo("f is 0")
: echo("f is not 0")

You will get a syntax error when you run the snippet above.

However, you may use other alternatives like (print, print_r, and var_dump) to output your variable.

  • In as much as a print keyword is supported, You still can't Nest ternary blocks with it.

Recall that: Nesting a ternary block is like adding else if and else to your condition

$f = 0;

($f == 0) ?
print("f is 0")
: ($f == 1)?
print("f is 1")
: '';

The snippet above reads,

"If variable f equals zero, output f is zero, else if variable f equals one, output f is one"

This snippet will not execute properly when you run it. You will get the output from the print statements repeated twice.

But if we replaced the print keyword with a var_dump or print_r, we will get the correct output.

$f = 0;

($f == 0) ?
var_dump("f is 0")
: ($f == 1)?
var_dump("f is 1")
: '';

The code will execute as expected because we expect the exact output "f is 0".

  • You can't Nest a ternary block and still execute multiple actions within it.

I think this is by far the most important aspect of using a ternary operator. But it doesn't work and I will prefer you use ternary operators for little code logics and use the native if/else statements for complex logic. This is unlike what's made available in JavaScript.

In Conclusion

Only use Ternary operators for a single condition in PHP, and you may execute multiple actions within that condition. But If you wish to add else if / else, I recommend you switch to the native if / else statement.

Maybe, the developers of PHP will update how PHP interprets ternary blocks on future PHP updates, but until then, I suggest you stick to the Native if / else statement

I believe that at this point, I have made you understand what ternary operators are and how you can use them in both PHP and JavaScript.

Thank You!


Created by

Simon Ugorji

Backend Developer | PHP | JS







Related Articles