Using the Ternary Operator in PHP

This tutorial takes you through how to use the ternary operator in the PHP programming language.

PHP Ternary Operator

The ternary operator in PHP is a conditional operator that allows you to execute a statement based on whether a condition is true or false.

Using the ternary operator is just like writing an “if…else” statement in PHP, but more concise and potentially more readable.

This PHP operator is best used when you need to assign a variable a value based on whether a condition is true or false. It allows you to cut down what would be five lines of code down to one.

Over the following few sections, you will learn the syntax of the ternary operator and the various ways that you can use this operator.

Syntax of the PHP Ternary Operator

Let us start by exploring the syntax of the ternary operator in PHP. The syntax will give you an idea of how the operator is written.

The ternary operator is written using a question mark (?), followed by a colon (:). These two symbols can be written separated like “? :” or using the shorting hand version with both symbols next to each other “?:“.

The behavior of the operator does change slightly depending on how it is written.

Longhand Syntax

Let us start with the separated version of the ternary operator in PHP. With this, you will have three different operands.

The first operand is the condition that PHP evaluates as true or false.

After that, you write a question mark (?), followed by the expression that PHP will run if the condition is true.

Finally, you will write a colon (:), followed by the expression you want to be executed if the condition was false.

CONDITION ? TRUE_EXPRESSION : FALSE_EXPRESSION;

If the condition (CONDITION) is true, PHP will return the left expression (TRUE_EXPRESSION).

If the condition (CONDITION) is false, PHP will return the right expression (FALSE_EXPRESSION).

Shorthand Syntax

The ternary operator also has a shorthand syntax that allows you to remove the middle expression. This shorthand version of the operator is useful when the condition you want to be evaluated contains the value you want to be returned.

First, you write a condition that PHP will evaluate. With the shorthand ternary operator in PHP, this condition is the value that PHP will return if the condition is true.

After the condition, you will write the question mark (?) followed immediately by the colon symbol (:).

Finally, write the expression you want to be evaluated if the condition is false.

CONDITION ?: FALSE_EXPRESSION;

If the condition (CONDITION) is true, PHP will return the condition expression.

If the condition (CONDITION) is false, PHP will return the right expression (FALSE_EXPRESSION).

Using the Ternary Operator in PHP

Now that we know the syntax of the ternary operator, let us show you a couple of ways that you can utilize this operator in PHP.

Basic Usage of the Ternary Operator in PHP

For our first example, we will explore the basic usage of the ternary operator within PHP.

We start this example by creating a variable called “$count” and assigning it the value 20.

On our following line, we use PHP’s ternary operator to assign a value to our “$text” variable. For our condition, we are checking whether our “$count” variable is less than 30.

If the value is less than 30, the PHP string "Count is less than 30" will be returned to the variable.

However, if the value is greater than or equal to 30, the variable will be set to "Count is greater than 30".

We use the echo statement in PHP to print the final value stored in our “$text” variable.

<?php

$count = 20;

$text = ($count < 30) ? "Count is less than 30" : "Count is greater than 30";

echo $text;

?>

Below is the result that you should get from the above PHP script.

You can see that PHP evaluated the ternary operator’s condition to be true, and the “$text” variable had the string "Count is less than 30" assigned to it.

Count is less than 30

Long-Form Alternative to the Ternary Operator

Now, if you were to write our above example using an if…else statement in PHP, it would look like what we have shown below.

You can immediately see how the ternary operator allows you to write more concise PHP code.

<?php

$count = 20;

if ($count < 30) {
    $text = "Count is less than 30";
} else {
    $text = "Count is greater than 30";
}

echo $text;

?>

Using the Shorthand Ternary Operator in PHP

As mentioned earlier, PHP also has a shorthand version of its ternary operator, allowing you to cut out the middle operand.

With the middle cut out, the “condition” turns into an element that PHP will return, as we will show shortly.

We start this example by creating a variable called “$tutorial” and assigning it the value “PHP Ternary Operator“.

In our following line, we define a variable called “$title“, where we use the shorthand ternary operator to define its value.

If the “$tutorial” variable is true, then its value will be returned and assigned to the “$title” variable.

However, if the variable evaluates to be false, then PHP will assign the “$title” variable the value "No Tutorial Title Set".

The echo statement will print out the value stored within the “$text” variable.

<?php

$tutorial = "PHP Ternary Operator";

$title = $tutorial ?: "No Tutorial Title Set";

echo $title;

?>

Below you can see that since our “$tutorial” variable had a valid value, it was evaluated to be true. This means the “$title” variable was assigned the value stored in the “$tutorial” variable.

PHP Ternary Operator

Long-Form Alternative to the Shorthand Ternary Operator

If you were to write this same example in PHP without using the shorthand ternary operator, it would look a bit like what we have shown below.

<?php 

$tutorial = "PHP Ternary Operator";

if ($tutorial) {
    $title = $tutorial;
} else {
    $title = "No Tutorial Title Set";
}

echo $title;

?>

Chaining the Short-Hand Ternary Operator

Let us start this section by saying you shouldn’t chain full ternary operators together. Its behavior is unlike other languages prior to PHP 8.0, and as of PHP 8.0, the behavior is unsupported altogether.

However, PHP does support chaining shorthand ternary operators, and it can be a useful behavior to utilize.

With the shorthand ternary operator, each expression will be evaluated from left to right. It will return the first expression that is not false.

The easiest way to showcase this behavior is with a quick series of chained short ternary operators. Running this example will quickly show you how PHP evaluates these chained operators.

<?php

echo 1 ?: 2 ?: 3 ?: 4;
echo 0 ?: 2 ?: 3 ?: 4;
echo 0 ?: 0 ?: 3 ?: 4;
echo 0 ?: 0 ?: 0 ?: 4;

?>

Below you can see the result produced by the previous example. With this, you can see how PHP evaluated each chained ternary operator.

1
2
3
4

Conclusion

Throughout this tutorial, we have shown you how to use the ternary operator within PHP.

This operator is incredibly useful for writing more readable and concise code. In addition, it is beneficial in helping you avoid using a larger if…else statement when they aren’t needed.

Please comment below if you have any questions about using the ternary operator.

To learn more, check out our many other PHP tutorials. Or check out our other coding guides if you want to learn a new language.

Leave a Reply

Your email address will not be published. Required fields are marked *