(Condition) ? (Statement1) : (Statement2);
<?php
$marks=40;
print ($marks>=40) ? "pass" : "Fail";
?>
$result = $condition ? 'foo' : 'bar';
// Both ternary and if/else returns the same result
// ternary
$result = $condition ? 'foo' : 'bar';
// if/else
if ($condition) {
$result = 'foo'
} else {
$result = 'bar'
}
$y = $x ? "true" : "false";
echo $color = $color ?? 'red'; //if value not exists then assign to them.
(conditional) ? (execute this when true) : (execute this when false);
# example
<?php
$age = 20;
print ($age >= 18) ? "Adult" : "Not Adult";
?>
# output
Adult
print ($marks>=40) ? "pass" : "Fail";
<?php
$x=4;
$y=3;
if(x>y)?echo"X is large": echo "y is large";
?>
$if = function($test, $true, $false)
{
return $test ? $true : $false;
};
echo "class='{$if(true, 'abc', 'def')}'";
$customer->user->fullName ?? ''
$customer->user->fullName ? $customer->user->fullName : ''
isset($customer->user->fullName) ? $customer->user->fullName : ''
<?php
$is_user_logged_in = false;
$title = $is_user_logged_in ? 'Logout' : 'Login';Code language: HTML, XML (xml)