PHP Ternary Operator
ADVERTISEMENTS
PHP ternary operator is a conditional operator, It is used to shorten condition (if / else) checking.
Syntax of the PHP ternary operator
(Condition) ? $x : $y
Output: If condition is true return $x else return $y.
Understand the ternary operator from these examples:
Example 1 where the condition is true:
<?php
$age = 25;
$var = $age > 22 ? 'John' : 'Marky';
echo $var;
?>
Output :
John
Example 2 where the condition is false:
<?php
$age = 25;
$var = $age > 28 ? 'John' : 'Marky';
echo $var;
?>
Output :
Marky