W3docs

switch

In PHP, the "switch" keyword is used to evaluate a value and perform different actions based on the value. It provides an alternative to using multiple "if"

The PHP switch Statement

The PHP switch statement evaluates one expression and runs the block of code that matches its value. It is the cleaner alternative to a long chain of if / elseif / else statements when you are comparing the same variable against many possible values.

This chapter covers the syntax, the all-important break keyword, intentional fall-through, the alternative endswitch syntax, and the loose-comparison gotcha that trips up most beginners.

Syntax

The basic syntax for a switch statement in PHP is as follows:

<?php
switch ($value) {
  case $value1:
    // Code block here
    break;
  case $value2:
    // Code block here
    break;
  default:
    // Code block here
}

PHP compares $value against each case from top to bottom. The first match wins: its block runs, and break exits the switch. If no case matches, the optional default block runs. default does not have to be last, but placing it last is the convention readers expect.

Why break matters

The break statement stops execution and exits the switch block. Without it, PHP keeps running the code in every following case until it hits a break or the end of the block — a behavior called fall-through. This is the single most common switch bug:

<?php
$role = "editor";

switch ($role) {
  case "admin":
    echo "Full access. ";
    // no break — execution "falls through" to the next case
  case "editor":
    echo "Can edit content. ";
    break;
  case "viewer":
    echo "Read only.";
    break;
}
// Output: Can edit content.

Here editor matches, prints its message, and the break cleanly exits. Forgetting that break after admin would let an admin keep running editor's code too. See break for the full keyword.

Intentional fall-through

Fall-through is not always a mistake. Stacking empty case labels lets several values share one block — a clean way to group conditions:

<?php
$month = 2;

switch ($month) {
  case 12:
  case 1:
  case 2:
    echo "Winter";
    break;
  case 3:
  case 4:
  case 5:
    echo "Spring";
    break;
  default:
    echo "Another season";
}
// Output: Winter

Examples

Let's look at some practical examples of how switch is used:

<?php

// Example 1
$dayOfWeek = "Wednesday";

switch ($dayOfWeek) {
  case "Monday":
  case "Tuesday":
  case "Wednesday":
  case "Thursday":
  case "Friday":
    echo "It's a weekday.";
    break;
  case "Saturday":
  case "Sunday":
    echo "It's a weekend.";
    break;
}

// Example 2
$grade = "B";

switch ($grade) {
  case "A":
    echo "Excellent!";
    break;
  case "B":
    echo "Good job!";
    break;
  case "C":
    echo "Could do better.";
    break;
  default:
    echo "Please enter a valid grade.";
}

Example 1 groups weekday names with stacked case labels to decide weekday vs. weekend. Example 2 maps a letter grade to a feedback message, with default catching any value that does not match.

The loose-comparison gotcha

By default switch compares with loose equality (==), so values are matched after type juggling — not with strict ===. This can produce surprising matches:

<?php
$value = "1";

switch ($value) {
  case 1:
    echo "Matched the integer 1!";
    break;
  default:
    echo "Matched default.";
}
// Output: Matched the integer 1!

The string "1" matches the integer case 1 because == converts the types before comparing. When you need strict type checking, reach for a chain of if / elseif using ===, or use the match expression (PHP 8+), which compares strictly and returns a value:

<?php
$grade = "B";

$message = match ($grade) {
  "A" => "Excellent!",
  "B" => "Good job!",
  "C" => "Could do better.",
  default => "Please enter a valid grade.",
};

echo $message;
// Output: Good job!

match has no fall-through and needs no break, which removes a whole class of bugs.

Alternative endswitch syntax

When embedding PHP inside HTML templates, the colon/endswitch form can read more naturally:

<?php switch ($status): ?>
  <?php case "active": ?>
    <span>Active</span>
    <?php break; ?>
  <?php default: ?>
    <span>Inactive</span>
<?php endswitch; ?>

Benefits

  • Readability — one switch is easier to scan than many if statements when you compare the same variable against several values.
  • Single evaluation — the switch expression is evaluated once, then compared against each case.

Practice

Practice
According to the content of the page at w3docs.com, under what circumstances can a 'switch' statement be used in PHP?
According to the content of the page at w3docs.com, under what circumstances can a 'switch' statement be used in PHP?
Was this page helpful?