PHP Switch Statement
Selecting from multiple options efficiently
🔀 What is a Switch Statement?
The switch statement selects one of many code blocks to execute based on a value. It's a cleaner alternative to multiple if...elseif statements when comparing a single variable against many values.
<?php
$day = "Monday";
switch ($day) {
case "Monday":
echo "Start of the week!";
break;
}
?>
Output:
Start of the week!
Switch Statement Components
switch Expression
The value or variable being evaluated, compared against each case value to find a match.
<?php
switch ($variable) {
// cases here
}
?>
case Labels
Specific values to compare against the switch expression, each defining a potential execution path.
<?php
case "value":
// code
break;
?>
break Statement
Exits the switch block after executing matched case code, preventing fall-through to subsequent cases.
<?php
case "value":
echo "Match!";
break; // Important!
?>
default Case
Optional fallback code that executes when no case matches, similar to else in if statements.
<?php
default:
echo "No match";
break;
?>
🔹 Basic Switch Statement
Compare a variable against multiple values:
<?php
$day = "Wednesday";
switch ($day) {
case "Monday":
echo "Start of the work week!";
break;
case "Tuesday":
echo "Second day of the week.";
break;
case "Wednesday":
echo "Midweek already!";
break;
case "Thursday":
echo "Almost Friday!";
break;
case "Friday":
echo "TGIF!";
break;
case "Saturday":
case "Sunday":
echo "It's the weekend!";
break;
default:
echo "Invalid day.";
break;
}
?>
Output:
Midweek already!
🔹 Switch with Numbers
Use switch statements with numeric values:
<?php
$month = 3;
switch ($month) {
case 1:
echo "January - New Year!";
break;
case 2:
echo "February - Valentine's Month";
break;
case 3:
echo "March - Spring begins";
break;
case 4:
echo "April - Spring showers";
break;
case 5:
echo "May - Flowers bloom";
break;
case 12:
echo "December - Holiday season";
break;
default:
echo "Month number: $month";
break;
}
?>
Output:
March - Spring begins
🔹 Multiple Cases, Same Code
Group multiple cases that execute the same code:
<?php
$grade = "B";
switch ($grade) {
case "A":
case "A+":
case "A-":
echo "Excellent work!";
break;
case "B":
case "B+":
case "B-":
echo "Good job!";
break;
case "C":
case "C+":
case "C-":
echo "Satisfactory.";
break;
case "D":
echo "Needs improvement.";
break;
case "F":
echo "Failed. Please try again.";
break;
default:
echo "Invalid grade.";
break;
}
?>
Output:
Good job!
🔹 Switch vs If...Elseif
Comparing switch with if...elseif statements:
<?php
$color = "blue";
// Using if...elseif
echo "Using if...elseif:<br>";
if ($color == "red") {
echo "Color is red";
} elseif ($color == "blue") {
echo "Color is blue";
} elseif ($color == "green") {
echo "Color is green";
} else {
echo "Unknown color";
}
echo "<br><br>";
// Using switch (cleaner for multiple values)
echo "Using switch:<br>";
switch ($color) {
case "red":
echo "Color is red";
break;
case "blue":
echo "Color is blue";
break;
case "green":
echo "Color is green";
break;
default:
echo "Unknown color";
break;
}
?>
Output:
Using if...elseif:
Color is blue
Using switch:
Color is blue
🔹 Importance of break Statement
See what happens without break (fall-through):
<?php
$number = 2;
echo "Without break:<br>";
switch ($number) {
case 1:
echo "One<br>";
case 2:
echo "Two<br>";
case 3:
echo "Three<br>";
default:
echo "Default<br>";
}
echo "<br>With break:<br>";
switch ($number) {
case 1:
echo "One<br>";
break;
case 2:
echo "Two<br>";
break;
case 3:
echo "Three<br>";
break;
default:
echo "Default<br>";
break;
}
?>
Output:
Without break:
Two
Three
Default
With break:
Two
🔹 Practical Example: Menu System
Real-world usage of switch for menu selection:
<?php
$action = "view";
switch ($action) {
case "create":
echo "Creating new record...";
// Code to create record
break;
case "view":
echo "Viewing records...";
// Code to display records
break;
case "edit":
echo "Editing record...";
// Code to edit record
break;
case "delete":
echo "Deleting record...";
// Code to delete record
break;
case "search":
echo "Searching records...";
// Code to search
break;
default:
echo "Invalid action. Please choose: create, view, edit, delete, or search.";
break;
}
?>
Output:
Viewing records...