Ternary operator in PHP
| Theory | |
| Example |
Theory
First comes the expression, then ?
After ? come two values with : between them.
If the expression is true (True) the left value is chosen, otherwise the right one.
(Expression) ? If_true : If_false
Example
<?php
echo (1 == 1) ? 'True' : 'False';
// True
echo (1 == 2) ? 'True' : 'False';
// False
$r = (1 == 1) ? 'Yes' : 'No'; // $r is set to 'Yes'
echo $r;
// Yes
Article author: Andrei Olegovich