The PHP Ternary Operator is a way to quickly express if/else statements. The ternary operator follows the following syntax:
( boolean expression ) ? if_true : if_false;
If the boolean expression evaluates to true, the “if_true” result is displayed, otherwise the “if_false” result is displayed.
Just as a quick recap, the normal if/else syntax looks like the following:
<?php
$expression = true; if ( $expression ) {
// do statement if true
} else {
// do statement if false
}
?>
This gets redundant especially when you’re just echoing statements. You will need to write 2 different echo statements: one for the if true portion and the other for the if false portion. The ternary operator can be squeezed into a single echo statement itself.
echo ( boolean expression ) ? if_true : if_false;
Let’s look at an example.
<?php
$money = 2000;
echo ( $money >= 2000 ) ? "Time to spend" : "Need more money";
?>
Since the variable $money contains 2000, the expression $money ≥ 2000 evaluates to true, so “Time to spend” is displayed on the screen. If the value that was stored inside of the $money variable was less than 2000, you would see “Need more money” on your screen.
Ternary operators are also great when you need to assign values to a variable after some decision is made.
<?php
$money = 2000;
$isBuyingAMountainBike = ( $money >= 2000 ) ? true : false;
?>
In the statement above, PHP:
Assigns 2000 to the $money variable.
Progresses to the $isBuyingAMountainBike variable. It sees that it contains a ternary operator.
PHP evaluates the expression, $money ≥ 2000. The expression returns true .
Since the expression returned true, PHP goes into the “if_true” portion of the ternary expression and sees that it contains the value true .
The value true is assigned to the $isBuyingAMountainBike variable.
We could have easily placed Yes/No or 1/0 values instead of true/false for the ternary expression above.
Let’s look at an example where the ternary operator really starts to shine. Imagine that we have a variable $employeeOrCustomer . We’re writing out an email and depending on the value of the variable, we want to either print a formal greeting, or a more laid back one. We’re going to store the whole message inside of the $msg variable. If we were not acquainted with the ternary operator, we might use the if/else statement to generate our $msg value.
<?php
$employeeOrCustomer = "employee" ;
$msg = "Hey " ;
if ( $employeeOrCustomer == "employee" ) {
$msg .= "dude" ;
} else {
$msg .= "sir/ma'am" ;
}
$msg .= ",<br><br>How are you?" ;
var_dump ($msg );
?>
The if/else statement was added so that it can make a decision. It’s trying to figure out whether it will append “dude” or “sir/ma’am” to the $msg value. We can achieve the same result with one statement thanks to the ternary operator.
<?php
$employeeOrCustomer = "employee" ;
$msg2 = "Hey " . ( ($employeeOrCustomer == "employee" ) ? "dude" : "sir/ma'am" ) . ",<br><br>How are you?" ;
var_dump ($msg2 );
?>
The ternary operator should not be abused; it should only be used in simple if/else decision making. It you have nested expressions, you should try to either convert the nested boolean expression into a single expression with the help of logical operators, like the && operator, or simply use the if/else structure.
<?php
if ( $employeeOrCustomer == "employee" ) {
echo "Yo. " ;
if ( $money >= 2000000 ) {
echo "I'm about to quit cause I'm rich" ;
}
}
?>
The ternary operator is something that you’ll use quite frequently throughout your programming career, so get used to it. You’ll love it.