PHP Ternary Operator are binary operators which combine two operands or expressions into a single but more complex expression. PHP has some unary and one ternary operators as well. Unary operators convert a single expression into a more complex expression and a ternary operator combines three expressions into a single expression.
Ternary Operator in PHP is faster and cleaner way through which we can write simple if/else statements.
$variable = condition ? if true : if false
Portion to the left of the ? is a condition of testing. Portion between the ? and the : is the result/output if the condition is true. Portion after the : is the result/output if the condition is false.
<?php $cow = "white"; $x = ($cow = "white") ? "Wow! This cow is of my favourite colour." : "Uhh! This cow's colour is not favourite one."; print $x; ?>
This will produce following result:
Wow! This cow is of my favourite colour.