ternary operator in php code example
Example 1: php ternary operator
(Condition) ? (Statement1) : (Statement2);
Example 2: ternary operator in php
<?php
$marks=40;
print ($marks>=40) ? "pass" : "Fail";
?>
Example 3: php ternary operator
<?php
(if Condition) ? (stat1) : (stat2);
$var1 = 5;
$var2 = 2;
echo $check = ($var1 > $var2) ? "right" : "wrong";
?>
Example 4: php ternary
$result = $condition ? 'foo' : 'bar';
Example 5: php ternary operators
$result = $condition ? 'foo' : 'bar';
if ($condition) {
$result = 'foo'
} else {
$result = 'bar'
}
Example 6: ternary operator for three conditions in php
$foo = your_value;
$bar = ($foo == 1) ? "1" : (($foo == 2) ? "2" : "other");
echo $bar;