TExcelTAD
Home › PHP › PHP Switch

PHP Switch

When you compare one value against many possible options, a long if/elseif chain becomes repetitive. The switch statement expresses the same idea more directly: evaluate an expression once, then jump to the case that matches.

Basic syntax

php
switch (expression) {
    case value1:
        // code when expression == value1
        break;
    case value2:
        // code when expression == value2
        break;
    default:
        // code when nothing matched
}
  • Each case is compared to the expression using loose comparison (==).
  • break leaves the switch. Without it, execution "falls through" into the next case.
  • default runs when no case matches. It is optional and usually placed last.
php · try it yourself
<?php
$day = "Sat";

switch ($day) {
    case "Mon":
        echo "Start of the work week\n";
        break;
    case "Fri":
        echo "Almost weekend!\n";
        break;
    case "Sat":
        echo "Weekend: time for a hike\n";
        break;
    default:
        echo "A regular weekday\n";
}
Weekend: time for a hike

Fall-through: grouping cases

Forgetting break is a classic bug, but fall-through is also useful on purpose: stack several case labels to share one block of code.

php · try it yourself
<?php
function dayType(string $day): string {
    switch ($day) {
        case "Sat":
        case "Sun":
            return "weekend";
        case "Mon":
        case "Tue":
        case "Wed":
        case "Thu":
        case "Fri":
            return "weekday";
        default:
            return "unknown";
    }
}

foreach (["Sun", "Wed", "Funday"] as $d) {
    echo "$d is a " . dayType($d) . "\n";
}
Sun is a weekend
Wed is a weekday
Funday is a unknown

Inside a function, return leaves both the switch and the function, so no break is needed.

What happens without break

Here is the accidental version. Once a case matches, PHP keeps running every following statement until it reaches a break or the end of the switch:

php · try it yourself
<?php
$level = 2;

switch ($level) {
    case 1:
        echo "Unlocked: bronze badge\n";
    case 2:
        echo "Unlocked: silver badge\n";
    case 3:
        echo "Unlocked: gold badge\n";
        break;
    case 4:
        echo "Unlocked: platinum badge\n";
}
Unlocked: silver badge
Unlocked: gold badge

Sometimes this is exactly what you want (a level-2 player earns every badge up to gold), but if it is intentional, leave a // fall through comment so readers know it isn't a mistake.

Loose comparison pitfalls

Because switch uses ==, values of different types may match. In PHP 8, 0 == "a" is false, but "1" == "01" and 100 == "1e2" are still true, because both sides are numeric strings or numbers.

php · try it yourself
<?php
$input = "1e2";   // a numeric string, e.g. from a form

switch ($input) {
    case 100:
        echo "Matched the integer 100\n";
        break;
    default:
        echo "No match\n";
}
Matched the integer 100

If you need strict comparison, use the match expression (next lesson) or a switch (true) with explicit === checks.

switch (true)

A common trick is to switch on true and write full conditions as cases. The first case whose condition evaluates to true wins:

php
switch (true) {
    case $bmi < 18.5: $label = "underweight"; break;
    case $bmi < 25:   $label = "normal";      break;
    default:          $label = "overweight";
}

switch vs if

Use switch when… Use if/elseif when…
You compare one value to fixed options Conditions involve different variables
Several options share the same code You need ranges like > 10 && < 20
You want a readable list of cases You need strict === comparison

Tip: In modern PHP, match is often a better fit than switch when each case just produces a value. Keep switch for cases that run several statements.

Test yourself

What comparison does switch use to test each case?

  1. Strict ===
  2. Loose ==
  3. Spaceship <=>
  4. Pattern matching

What happens when a matching case has no break?

  1. PHP throws an error
  2. The switch restarts
  3. Execution falls through into the next case
  4. Only the default runs

When does the default block run?

  1. Always, at the end
  2. When no case matched
  3. When the first case matched
  4. Only if it is written first