Get DevWP - WordPress Development Theme

PHP Ternary Operator Explained

PHP provides multiple ways to deal with decision making and controlling the flow of your program.

Besides the if else statement that’s widely used for conditional checks, we also have the more compact Ternary Operator which gives you the same result as the more verbose if else statement.

The PHP ternary operator is passed an expression to evaluate, along with two statements of which one will be chosen depending on the result of the expression evaluation: the first is for when the expression evaluates to true and the second for when it is false.

condition ? result for true : result for false;

As you can see from the pseudocode above, we first have a condition that needs to be evaluated, then we have the ? followed by the value if the expression is true, then we have the : for when the condition evaluates to false.

Below is a real example of the ternary operator you can try in your editor.

$weather = 'snowing';
echo $weather == 'raining' ? 'It is raining outside' : 'It is snowing outside';

The result is we end up echoing out the string It is snowing outside.

The more verbose, if/else version

We can use the if/else to get the same result but it will take more lines of code to get the job done.

if ( $weather == 'raining' ) {
	echo 'It is raining outside';
} else {
	echo 'It is snowing outside';

Assign the Ternary Operator Result to a Variable

$current_weather = $weather == 'raining' ? 'It is raining outside' : 'It is snowing outside';
echo $current_weather;

The result is we take the value from the ternary operator and assign it to the $current_weather variable and then we echo out the variable.

What are the Benefits of the Ternary Operator?

  • uses less lines of code than the if/else
  • you can do your conditional checks inline which will definitely come in handy
  • once you understand how to use it, your code will be easier to maintain

Inline example of the ternary operator

$color = 'red';
echo 'your color is ' . ($color == 'blue' ? 'blue' : 'red');

As you can see from the example above, we first create a variable $color and assign it the value of red, then we echo out the result from the ternary operator inline.

Notice how we echoed out the first part your color is followed by a period to which is used for concatenation and then in parenthesis we had ($color == ‘blue’ ? ‘blue’ : ‘red’). We did it that way because of how we combined the string and the ternary for the inline demonstration.

Example below won’t give you your intended results

You might think the code below will achieve the same results, but it won’t.

$color = 'red';
echo 'your color is ' . $color == 'blue' ? 'blue' : 'red';

The result from the example above will just be red and not the full string you intended to output.

The reason for is because of the order of operations, remember PEMDAS. In order to ensure we can output our full string with the result of the ternary operator, we have to use parenthesis. It comes down to how PHP handles the concatenation operator.

Refactor the PHP Ternary Operator for inline use with Echo Arguments

You can refactor the code above by using a comma instead of a period which will make what’s left and right of the comma arguments. It’s easier to show you the code.

$color = 'red';
echo 'your color is ', $color == 'blue' ? 'blue' : 'red';

The output from the example above is your color is red which is the result we want for our code logic.

Hopefully you now have a better understanding of how to use the PHP Ternary Operator and how it can be used to simplify your conditional logic. As always, thank you for reading this article.

View Our Themes