The switch statement is used to execute different
blocks of code depending on the value of an expression.
It is useful when you need to compare one value against
several possible cases.
A switch statement checks an expression
against different case values. When a matching case
is found, its code executes.
Syntax:
switch ($variable) {
case value1:
// Code for value1
break;
case value2:
// Code for value2
break;
default:
// Code when no case matches
}
The break statement stops execution
from continuing into the following cases.
Let us display a message based on a day number.
<?php
$day = 3;
switch ($day) {
case 1:
echo "Monday";
break;
case 2:
echo "Tuesday";
break;
case 3:
echo "Wednesday";
break;
default:
echo "Invalid day";
}
?>
Output:
Wednesday
Because $day is 3, PHP executes
the third case and stops at break.
The break statement exits the switch
after the matching case has executed.
<?php
$number = 1;
switch ($number) {
case 1:
echo "One ";
break;
case 2:
echo "Two ";
break;
case 3:
echo "Three ";
break;
}
?>
Output:
One
PHP executes the code for case 1 and exits the
switch because of break.
If you omit break, PHP can continue
executing subsequent case blocks until it reaches
a break or the end of the switch.
This behavior is called fall-through.
The default case runs when no case
matches the switch expression.
<?php
$color = "Purple";
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 "Color not found";
}
?>
Output:
Color not found
Since none of the listed cases matches
"Purple", the default block executes.
Multiple cases can share the same block of code. This is useful when different values should produce the same result.
<?php
$day = "Sunday";
switch ($day) {
case "Saturday":
case "Sunday":
echo "Weekend";
break;
default:
echo "Working day";
}
?>
Output:
Weekend
Both Saturday and Sunday lead to the same message because neither case has a statement before the shared block.
A switch statement can also work with a calculated or previously assigned value.
<?php
$grade = "B";
switch ($grade) {
case "A":
echo "Excellent";
break;
case "B":
echo "Very Good";
break;
case "C":
echo "Good";
break;
case "D":
echo "Needs Improvement";
break;
default:
echo "Invalid grade";
}
?>
Output:
Very Good
The value of $grade matches case B,
so PHP displays "Very Good".
You can create a simple calculator by checking which arithmetic operator the user has selected.
<?php
$a = 20;
$b = 5;
$operator = "*";
switch ($operator) {
case "+":
echo $a + $b;
break;
case "-":
echo $a - $b;
break;
case "*":
echo $a * $b;
break;
case "/":
if ($b != 0) {
echo $a / $b;
} else {
echo "Cannot divide by zero";
}
break;
default:
echo "Invalid operator";
}
?>
Output:
100
Since the operator is *, PHP
multiplies the two numbers.
In real applications, validate user input and handle division by zero before performing division.
Switch is useful for selecting an action based on a string value, such as a menu choice.
<?php
$choice = "about";
switch ($choice) {
case "home":
echo "Welcome to the Home Page";
break;
case "about":
echo "Welcome to the About Page";
break;
case "contact":
echo "Welcome to the Contact Page";
break;
default:
echo "Page not found";
}
?>
Output:
Welcome to the About Page
In PHP, traditional switch comparisons use loose comparison rules. Case matching is not automatically case-insensitive for strings, so "about" and "About" should not be assumed to match.
| Switch | If-Else |
|---|---|
| Useful for comparing one expression against several case values. | Useful for simple or complex Boolean conditions. |
| Uses case labels. | Uses conditions. |
| Uses break to prevent fall-through. | Does not require break between branches. |
| Can include a default case. | Can include an else block. |
Example using if-else:
<?php
$age = 20;
if ($age >= 18) {
echo "Adult";
} else {
echo "Minor";
}
?>
Output:
Adult
Use if-else when you need to test
ranges or complex conditions. Use switch
when selecting between several possible values.
Write PHP programs to complete these exercises:
Sample starting code:
<?php
$month = 4;
switch ($month) {
case 1:
echo "January";
break;
case 2:
echo "February";
break;
case 3:
echo "March";
break;
case 4:
echo "April";
break;
default:
echo "Invalid month";
}
?>
Expected output:
April
1. Which keyword is used to define a case in a switch statement?
2. What is the purpose of break in a switch statement?
3. Which block runs when no case matches?