0Pricing
PHP Academy · Lesson

Switch and Match Statements

Use switch and PHP 8 match for multi-branch logic.

Switch and Match Statements is a free PHP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Switch vs if Chains

When you have many branches based on a single variable's value, switch is cleaner than a long if/elseif chain.

PHP 8 introduced match as a modern, strict alternative.

Switch Statement Basics

Switch evaluates its expression once and runs the matching case block:

<?php
$day = 'Monday';

switch ($day) {
    case 'Monday':
        echo 'Start of work week';
        break;
    case 'Friday':
        echo 'Almost weekend';
        break;
    default:
        echo 'Midweek';
}

Fall-Through Behavior

Without break, execution falls through to the next case — intentional grouping trick:

<?php
$status = 2;

switch ($status) {
    case 1:
    case 2:
    case 3:
        echo 'In progress';  // runs for 1, 2, or 3
        break;
    case 4:
        echo 'Done';
        break;
}

Switch Uses Loose Comparison

Switch uses == (loose comparison) internally — this can cause surprising matches:

<?php
$val = 0;

switch ($val) {
    case 'foo':   // 0 == 'foo' in PHP 7!
        echo 'match foo';
        break;
    case 0:
        echo 'match zero';  // PHP 8 fixes this
        break;
}
// PHP 8: match zero (fixed)
// PHP 7: match foo (bug!)

PHP 8 match Expression

match is like switch but uses strict comparison, returns a value, and requires no break:

<?php
$status = 'active';

$label = match($status) {
    'active'   => 'Currently active',
    'inactive' => 'Not active',
    'pending'  => 'Awaiting activation',
    default    => 'Unknown status',
};

echo $label; // Currently active

match with No-Match Exception

If no arm matches in match and there's no default, it throws UnhandledMatchError:

<?php
$code = 99;

try {
    $result = match($code) {
        200 => 'OK',
        404 => 'Not Found',
        500 => 'Server Error',
        // no default!
    };
} catch (\UnhandledMatchError $e) {
    echo 'Unhandled match value: ' . $code;
}

match with Multiple Conditions

A single match arm can list multiple comma-separated values:

<?php
$lang = 'tr';

$region = match($lang) {
    'en', 'en-US', 'en-GB' => 'English',
    'tr', 'az'             => 'Turkic',
    'fr', 'be', 'ca'       => 'French-speaking',
    default                => 'Other',
};

echo $region; // Turkic

match with Complex Expressions

match arms can use arbitrary expressions on the right-hand side:

<?php
$score = 85;

$grade = match(true) {
    $score >= 90 => 'A',
    $score >= 80 => 'B',  // matches
    $score >= 70 => 'C',
    default      => 'F',
};

echo $grade; // B

return from match

Because match is an expression it can be used anywhere a value is expected:

<?php
function getHttpMessage(int $code): string {
    return match($code) {
        200 => 'OK',
        201 => 'Created',
        400 => 'Bad Request',
        401 => 'Unauthorized',
        404 => 'Not Found',
        default => 'Unknown',
    };
}

echo getHttpMessage(404); // Not Found

switch in return Context

switch can be used inside a function to return different values, but match is cleaner:

<?php
function describe(int $n): string {
    switch (true) {
        case $n < 0:  return 'negative';
        case $n === 0: return 'zero';
        default:       return 'positive';
    }
}

echo describe(-5);  // negative
echo describe(0);   // zero
echo describe(7);   // positive

When to Use switch vs match

Guidelines for choosing:

  • Use match when you need strict comparison and a return value (PHP 8+)
  • Use switch for fall-through grouping or when supporting PHP 7
  • Both are cleaner than long if/elseif chains for multi-value branching

Quick Check

What happens in a match expression when no arm matches and there is no default?

Recap: switch and match

Key differences:

  • switch — loose comparison, fall-through, PHP 4+
  • match — strict comparison, no fall-through, returns value, PHP 8+
  • match throws UnhandledMatchError with no default
  • match arms accept multiple comma-separated values

Next: for and while loops.

Frequently asked questions

Is the “Switch and Match Statements” lesson free?

Yes — the full text of “Switch and Match Statements” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Switch and Match Statements”?

Use switch and PHP 8 match for multi-branch logic. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Switch and Match Statements” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. if, elseif, and else
  2. Switch and Match Statements
  3. for and while Loops
  4. foreach and Loop Control
← Back to PHP Academy