← Back to PHP Course | Chapter 4: Control Flow | Lesson 7 of 14

PHP switch Statement

A switch is like a vending machine keypad: you press one code and it gives the matching item. PHP compares one value against a list of choices and runs the one that matches.
Syntax
php
switch ($variable) {
    case value1:
        // ...
        break;
    case value2:
        // ...
        break;
    default:
        // no case matched
}

Basic switch Statement

switch compares one variable against a list of fixed candidate values, which reads more cleanly than an equivalent elseif ladder once you're checking more than two or three specific values against the same variable.

Example: Basic switch Statement

php
<?php
// Declare `$day`, set to "Tuesday"
$day = "Tuesday";
// Switch on `$day` and branch by matching case
switch ($day) {
    // Case: `"Monday"`
    case "Monday":
        // Print "Start of week" to the output
        echo "Start of week";
        break;
    // Case: `"Tuesday"`
    case "Tuesday":
        // Print "Second day" to the output
        echo "Second day";
        break;
}
?>

The Importance of break

Without an explicit break, execution 'falls through' into the next case's code after a match is found, running it too even though its own condition was never checked — a frequent source of bugs, and the main reason break is treated as mandatory at the end of nearly every case.

Example: The Importance of break

php
<?php
// Declare `$day`, set to "Monday"
$day = "Monday";
// Switch on `$day` and branch by matching case
switch ($day) {
    // Case: `"Monday"`
    case "Monday":
        // Print "Monday\n" to the output
        echo "Monday\n";
    // Case: `"Tuesday"`
    case "Tuesday":
        // Print "Tuesday (falls through!)\n" to the output
        echo "Tuesday (falls through!)\n";
        break;
}
?>

Default Switch Case

default runs when the tested value doesn't match any listed case — functionally the same role else plays at the end of an elseif ladder, catching whatever wasn't explicitly handled above it.

Example: Default Switch Case

php
<?php
// Declare `$fruit`, set to "mango"
$fruit = "mango";
// Switch on `$fruit` and branch by matching case
switch ($fruit) {
    // Case: `"apple"`
    case "apple":
        // Print "Apple" to the output
        echo "Apple";
        break;
    // Default case, if nothing above matched
    default:
        // Print "Unknown fruit" to the output
        echo "Unknown fruit";
}
?>

Grouping Switch Cases

Listing several case labels back-to-back with no code between them, followed by a single shared block, lets multiple distinct values trigger identical behavior without duplicating that block for each one.

Example: Grouping Switch Cases

php
<?php
// Declare `$day`, set to "Saturday"
$day = "Saturday";
// Switch on `$day` and branch by matching case
switch ($day) {
    // Case: `"Saturday"`
    case "Saturday":
    // Case: `"Sunday"`
    case "Sunday":
        // Print "Weekend" to the output
        echo "Weekend";
        break;
    // Default case, if nothing above matched
    default:
        // Print "Weekday" to the output
        echo "Weekday";
}
?>

Alternative Colon Syntax

Like if, switch supports a colon-based alternative syntax closed with endswitch;, which keeps templates that mix PHP and HTML more readable than the brace-heavy default form would.

Example: Alternative Colon Syntax

php
<?php
// Declare `$role`, set to "admin"
$role = "admin";
// Switch on `$role` and branch by matching case
switch ($role):
    // Case: `"admin"`
    case "admin":
        // Print "Admin panel" to the output
        echo "Admin panel";
        break;
    // Default case, if nothing above matched
    default:
        // Print "No access" to the output
        echo "No access";
endswitch;
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting break, so execution falls through and runs the following case's code as well.
  2. Expecting switch to compare strictly, when it uses ==, so "0" and 0 can match the same case.
  3. Leaving out default, so unexpected values silently do nothing.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.