$age = 23;
$result = match (true) {
$age >= 65 => 'senior',
$age >= 25 => 'adult',
$age >= 18 => 'young adult',
default => 'kid',
};
echo $result; # => young adult
PHP Conditionals
Match expressions
Ternary operator
# => Does
print (false ? 'Not' : 'Does');
$x = false;
# => Does
print($x ?: 'Does');
$a = null;
$b = 'Does print';
# => a is unset
echo $a ?? 'a is unset';
# => print
echo $b ?? 'b is unset';
Switch
$x = 0;
switch ($x) {
case '0':
print "it's zero";
break;
case 'two':
case 'three':
// do something
break;
default:
// do something
}
If elseif else
$a = 10;
$b = 20;
if ($a > $b) {
echo "a is bigger than b";
} elseif ($a == $b) {
echo "a is equal to b";
} else {
echo "a is smaller than b";
}
Comments