beginner18 min

Control Flow: if, foreach, while, and match

PHP's conditionals and loops, plus PHP 8's more precise `match` expression.

What you'll learn

  • Write `if`/`elseif`/`else` and iterate an array with `foreach`
  • Explain how `match` differs from PHP's older `switch` statement
  • Predict the output of a `foreach` loop combined with a `match` expression

Explanation

PHP's if/elseif/else looks familiar from most C-family languages: if ($age >= 18) { ... } elseif ($age >= 13) { ... } else { ... }. while covers a condition-checked loop, and foreach is PHP's idiomatic way to iterate an array: foreach ($items as $item) { ... }, or foreach ($map as $key => $value) { ... } when you need both the key and value.

PHP has long had a switch statement, but PHP 8 added match, a more precise expression (it produces a value, unlike switch) with two important differences: match compares using strict equality (===, no type coercion), and it never falls through between arms -- each arm is a single condition => result pair, with no break needed and no risk of accidentally falling into the next case.

A common idiom is match (true) { condition1 => result1, condition2 => result2, default => fallback } -- since each arm's left side is compared against true with ===, this lets you write arbitrary boolean conditions per arm rather than being limited to matching a single value, which is what most people actually want from a "smarter switch."

Because match has no fallthrough and uses strict comparison, it tends to produce fewer of the subtle bugs switch was historically known for -- though switch still shows up often in existing PHP code, so recognizing both is worthwhile.

Guided lab

Fill in the blank: foreach with a match expression

PHPNot executed
This lab does not run in your browser or on VisaSparkSchools's servers. Read the code, fill in the missing piece, then reveal the completed code and its expected output.

Fill in the missing loop keyword, then predict the output.

<?php
$numbers = [1, 2, 3, 4, 5];

____ ($numbers as $n) {
    $label = match (true) {
        $n % 2 === 0 => "even",
        default => "odd",
    };
    echo "$n $label\n";
}

Stuck? Get a hint.

Common mistakes

  • Expecting `match` arms to fall through to the next arm the way un-`break`-ed `switch` cases can in some languages -- `match` never falls through.
  • Forgetting `match` compares with strict equality (`===`), so a numeric string arm won't match an int value the way a loose `switch` case might.
  • Writing `foreach ($item as $items)` (reversed) instead of `foreach ($items as $item)`, mixing up which side is the array being iterated.

Knowledge check

Knowledge check

1. How does PHP's `match` compare its arms against the subject value?
2. Does a `match` expression need an explicit `break` after each arm?
3. In `foreach ($numbers as $n) { ... }`, what does `$n` refer to on each iteration?

Takeaway

Prefer `match` over `switch` when you want strict comparison and no fallthrough risk, and use `foreach ($items as $item)` as PHP's idiomatic array-iteration loop.

Summary

`if`/`elseif`/`else` and `while` behave as expected; `foreach` iterates arrays; `match` is a strict, fallthrough-free expression, often combined with `match (true)` for arbitrary conditions.

References

Your notes

Notes save automatically.

Finished this lesson?

Mark it complete to track your progress and schedule a future review.