Switch Statement in PHP – Simple and Complete Guide

Learn how to use the switch statement in PHP. Master switch syntax, fall-through, ranges, and when to use match instead with practical examples.

PHP COURSE
PHP – conditional statements: if, else, elseif, switch, and match
Lesson

The switch statement in PHP is a convenient way to choose one of many code paths based on the value of a single variable or expression. It makes the code more readable and easier to maintain compared to long chains of if, elseif, and else. This is especially useful when comparing the same value against many possible variants (e.g., file type, order status, user role).

In this lesson, you’ll learn the basics of the switch statement in PHP, different usage patterns, good programming practices, and the most common mistakes. You’ll also see numerous code examples with comments so you can apply the knowledge right away.


Basics: what is switch and how does it work in PHP?

The switch statement checks the value of an expression and compares it sequentially against the values defined in each case. When it finds a match, it executes the code inside that case. Usually, we end the block with the break keyword to stop further checking.

Key points:

  • In PHP, comparisons in switch are loose (==) by default, not strict (===). This matters for data types.
  • The optional default block runs if no case matches.
  • Intentional fall-through (executing subsequent cases) is possible, but should be used carefully and commented.
---

Syntax and Step-by-Step Execution

Basic syntax

<?php
$value = 2;

switch ($value) {
    case 1:
        echo "Chosen 1";
        break; // stop further checking

    case 2:
        echo "Chosen 2";
        break;

    case 3:
        echo "Chosen 3";
        break;

    default:
        echo "No match";
        // break in default is optional
}

How it works:

    • PHP compares $value to 1, then 2, then 3.
    • When it finds a matching case, it executes that code until a break or the end of the switch.
    • If no match, it executes the default.
---

Practical Examples in PHP

1) Switch with string values

<?php
$fileType = 'jpg';

switch ($fileType) {
    case 'png':
        echo "PNG file – handled by GD.";
        break;

    case 'jpg':
    case 'jpeg':
        // Grouping multiple cases to one action
        echo "JPEG file – lossy compression.";
        break;

    case 'gif':
        echo "GIF file – limited color palette.";
        break;

    default:
        echo "Unknown file type.";
}

  • Several case labels in a row allow handling multiple values the same way.
  • A common PHP pattern for file types, roles, statuses.

2) Intentional fall-through

<?php
$level = 'warning';

switch ($level) {
    case 'debug':
        // no break – intentionally fall through to 'info'
    case 'info':
        // no break – intentionally fall through to 'warning'
    case 'warning':
        echo "Print messages at least warning level.";
        break;

    case 'error':
        echo "Only critical errors.";
        break;

    default:
        echo "Unknown level.";
}

⚠️ Always add a comment when fall-through is intentional.

3) Switch for ranges – switch(true) pattern

PHP doesn’t support ranges directly in case, but you can use the switch (true) trick:

<?php
$age = 17;

switch (true) {
    case ($age < 13):
        echo "Child";
        break;

    case ($age >= 13 && $age < 18):
        echo "Teenager";
        break;

    case ($age >= 18 && $age < 65):
        echo "Adult";
        break;

    default:
        echo "Senior";
}

This is a readable alternative to multiple if/elseif chains.

4) Alternative syntax with colons and endswitch

<?php
$role = 'editor';
?>

<?php switch ($role): ?>
    <?php case 'admin': ?>
        <p>Admin panel</p>
        <?php break; ?>

    <?php case 'editor': ?>
        <p>Editor panel</p>
        <?php break; ?>

    <?php default: ?>
        <p>Welcome, user!</p>
<?php endswitch; ?>

Useful in template files (HTML + PHP).

5) Loose comparisons (==) in switch

<?php
$value = '0'; // string

switch ($value) {
    case 0:
        echo "Matched number 0 (== true for '0')";
        break;

    case false:
        echo "Also matches in loose comparison!";
        break;

    default:
        echo "No match.";
}

Since switch uses loose comparisons, '0', 0, and false may unexpectedly match. Use if with === or match in PHP 8 for strict comparisons.

6) Mapping values – returning results

<?php
$country = 'PL';
$currency = null;

switch ($country) {
    case 'PL':
        $currency = 'PLN';
        break;

    case 'US':
        $currency = 'USD';
        break;

    case 'UK':
    case 'GB':
        $currency = 'GBP';
        break;

    default:
        $currency = 'EUR';
}

echo $currency; // PLN

7) Switch with enums (PHP 8.1+)

<?php
enum Status: string {
    case Draft = 'draft';
    case Published = 'published';
    case Archived = 'archived';
}

function statusLabel(Status $status): string {
    switch ($status) {
        case Status::Draft:
            return 'Draft';
        case Status::Published:
            return 'Published';
        case Status::Archived:
            return 'Archived';
        default:
            return 'Unknown';
    }
}

echo statusLabel(Status::Published); // Published


Best Practices and Common Mistakes

Best Practices

  • Always add break at the end of case, unless fall-through is intentional (and comment it).
  • Use a default block to handle unexpected values.
  • Group similar cases to avoid duplication:
  case 'jpg':
  case 'jpeg':
      // handle both
  • Use constants or enums instead of magic strings for safety.
  • When returning a value, consider returning directly in the case or using match (PHP 8+) for strict comparisons.
  • For ranges, use the switch(true) pattern or if/elseif.
  • Keep case blocks small — move logic into functions or methods.

Common Mistakes

  • Forgetting break, causing unintended fall-through.
  • Not realizing switch uses loose comparisons (==), leading to unexpected matches ('0', 0, false, '').
  • Overly large switches with dozens of cases — better use associative arrays or match.
  • Missing default, leaving inputs unhandled.
  • Using switch where an array map would be clearer.

When to consider match (PHP 8+)

The match expression:

  • Uses strict comparisons (===).
  • Returns a value.
  • Requires full coverage of cases (or default).
Example:

<?php
$country = 'PL';

$currency = match ($country) {
    'PL' => 'PLN',
    'US' => 'USD',
    'UK', 'GB' => 'GBP',
    default => 'EUR',
};

echo $currency; // PLN

match doesn’t fully replace switch (e.g., for side effects), but often simplifies code when mapping values.


Summary

  • The switch statement simplifies logic when testing a variable against many cases.
  • Remember: switch uses loose comparisons (==).
  • Use break and default.
  • For ranges, use switch(true) or if/elseif.
  • For returning values with strict comparisons, use match.
  • Ensure readability by grouping cases, using constants/enums, and extracting long logic.
---

Mini Quiz

    • What will this print and why?
<?php
$val = '0';

switch ($val) {
    case 0:
        echo "A";
        break;
    case false:
        echo "B";
        break;
    default:
        echo "C";
}

➡️ A, because '0' == 0.

    • Complete the switch so that 'jpg' and 'jpeg' print “JPEG image”, 'png' → “PNG image”, and others → “Unknown format”.
    • Is there a logical bug here?

<?php
$role = 'editor';

switch ($role) {
    case 'admin':
        echo "Admin panel";
    case 'editor':
        echo "Editor panel";
        break;
    default:
        echo "User panel";
}

➡️ Yes, missing break after 'admin'.

    • Which is safer for strict comparisons and returning values: switch or match? ➡️ match
    • Write a switch(true) that classifies temperature:

  • below 0 → “Freezing”


    • 0–20 → “Cold”

    • 21–30 → “Warm”

    • above 30 → “Hot”

    ---

    By practicing, you’ll master the switch statement and know when to replace it with match or arrays for cleaner PHP code.

    Back to Instant results from learning PHP