Tsallaka zuwa abun ciki
Justice Tech School

If, Else and Match

if, elseif and else choose between branches. switch compares one value with several cases (and needs break). The newer match expression is stricter and returns a value, so it is usually the better choice.

Example
$score = 72;
if ($score >= 85) {
  echo "Distinction\n";
} elseif ($score >= 70) {
  echo "Pass\n";
} else {
  echo "Retake\n";
}
$day = 3;
$name = match ($day) {
  1 => "Monday",
  2 => "Tuesday",
  3 => "Wednesday",
  default => "Another day",
};
echo $name, "\n";

Result

Pass
Wednesday

Exercise

Add a second condition.

if ($a) {}  ($b) {}

Quiz Test

What does match return?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.