PHP Switch
PHP switch Statement
Section titled “PHP switch Statement”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.
The PHP switch Statement
Section titled “The PHP switch Statement”Use the switch statement to select one of many code blocks to execute based on a comparison.
Syntax
Section titled “Syntax”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}How it Works:
Section titled “How it Works:”- The
expression(often a variable) is evaluated once. - The value of the
expressionis compared with the values specified in eachcasestatement. - Important: The comparison is loose (
==), meaning type juggling might occur (e.g.,0 == 'a'is true). - If a match is found, the block of code associated with that
caseis executed. - The
breakstatement is crucial. It stops the execution inside theswitchblock. Withoutbreak, execution will “fall through” to the nextcaseblock, regardless of whether its value matches. - The optional
defaultcase is executed if none of thecasevalues match theexpression.
Example:
Section titled “Example:”<?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:
matchis an expression, meaning it returns a value that can be assigned to a variable or used directly. - No Fall-Through: Doesn’t require
breakstatements; only the code corresponding to the first matching arm is executed. - Requires Exhaustive Cases (or
default): Throws anUnhandledMatchErrorif the expression doesn’t match any arm and there’s nodefault. - 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.