Skip to content

PHP Tutorial Conditional Statements

The switch Statement

Lesson 89 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

switch is the older way to compare one value against many options. You will see it a lot in existing code, so you should know how it works.

index.php
PHP
<?php
$day = "Tue";

switch ($day) {
    case "Mon":
        echo "Start of the week";
        break;
    case "Tue":
    case "Wed":
        echo "Midweek";
        break;
    case "Sat":
    case "Sun":
        echo "Weekend";
        break;
    default:
        echo "Another day";
}
Output
OUTPUT
Midweek

The break statement

Each case needs a break. Without it, PHP keeps running the next case’s code too. That is called fall-through.

index.php
PHP
<?php
$n = 1;

switch ($n) {
    case 1:
        echo "one ";
    case 2:
        echo "two ";
        break;
    case 3:
        echo "three";
}
Output
OUTPUT
one two

case 1 had no break, so PHP ran into case 2. Sometimes this is intended (like "Tue" and "Wed" above), but it is a common source of bugs.

The default case

default runs when no case matches. It is optional, and usually goes last.

Loose comparison

switch compares with ==, not ===. Strings that look like numbers can match unexpectedly.

index.php
PHP
<?php
$input = "1";

switch ($input) {
    case 1:
        echo "matched the integer 1";
        break;
    default:
        echo "no match";
}
Output
OUTPUT
matched the integer 1

switch vs match

switchmatch
ComparisonLoose (==)Strict (===)
Returns a valueNoYes
Needs breakYesNo
Fall-throughYesNo
No matchSilently does nothingUnhandledMatchError
Runs several statementsYesOne expression per arm

When switch is still the right tool

Use switch when each branch needs several statements, or when you work on PHP 7. Otherwise match is safer.

warning
Forgetting break
The most common switch bug is a missing break. Your editor may warn you. Always check each case ends with break, return or a comment saying the fall-through is on purpose.