Skip to content

PHP Switch

The switch statement provides a way to execute different blocks of code based on the value of a single expression. It’s often used as an alternative to a series of if...elseif...else statements.

Use the switch statement to select one of many code blocks to execute based on a comparison.

switch (expression) {
case value1:
// Code to execute if expression == value1
break; // Exits the switch statement
case value2:
// Code to execute if expression == value2
break;
case value3:
// Code to execute if expression == value3
break;
// ... more cases
default:
// Code to execute if expression matches none of the above cases
}
  1. The expression (often a variable) is evaluated once.
  2. The value of the expression is compared with the values specified in each case statement.
  3. Important: The comparison is loose (==), meaning type juggling might occur (e.g., 0 == 'a' is true).
  4. If a match is found, the block of code associated with that case is executed.
  5. The break statement is crucial. It stops the execution inside the switch block. Without break, execution will “fall through” to the next case block, regardless of whether its value matches.
  6. The optional default case is executed if none of the case values match the expression.
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break; // Stop execution here
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
// Output: Your favorite color is red!
echo "<br>";
$day = "Monday";
switch ($day) {
case "Saturday":
case "Sunday":
echo "It's the weekend!";
break; // Exit after weekend check
case "Monday":
echo "Start of the work week.";
// No break here - demonstrates fall-through
case "Tuesday":
case "Wednesday":
case "Thursday":
echo " Still working..."; // This will also execute if day is Monday
break;
case "Friday":
echo "Almost weekend!";
break;
default:
echo "Invalid day.";
}
// Output: Start of the work week. Still working...
?>

PHP 8 match Expression (Modern Alternative)

Section titled “PHP 8 match Expression (Modern Alternative)”

PHP 8.0 introduced the match expression, which is often a more concise and safer alternative to switch:

  • Strict Comparison: Uses strict comparison (===), avoiding unexpected type juggling issues.
  • Expression-Based: match is an expression, meaning it returns a value that can be assigned to a variable or used directly.
  • No Fall-Through: Doesn’t require break statements; only the code corresponding to the first matching arm is executed.
  • Requires Exhaustive Cases (or default): Throws an UnhandledMatchError if the expression doesn’t match any arm and there’s no default.
  • Allows Multiple Conditions: Conditions can be comma-separated.

match Example (Equivalent to the color example):

Section titled “match Example (Equivalent to the color example):”
<?php
$favcolor = "red";
$message = match ($favcolor) {
"red" => "Your favorite color is red!",
"blue" => "Your favorite color is blue!",
"green" => "Your favorite color is green!",
default => "Your favorite color is neither red, blue, nor green!",
};
echo $message;
// Output: Your favorite color is red!
?>

While switch remains useful, consider using match in PHP 8+ for clearer and safer conditional logic, especially when you need strict comparisons and a return value.