Skip to content

PHP Switch Statement

Syntax

The switch statement in PHP is used to evaluate a single expression and execute code blocks based on matching cases.

switch (expression) {
case value1:
// Code to execute if expression equals value1
break;
case value2:
// Code to execute if expression equals value2
break;
default:
// Code to execute if no case matches
}

Example

$day = "Monday";
switch ($day) {
case "Monday":
echo "Start of the work week!";
break;
case "Friday":
echo "Almost the weekend!";
break;
case "Saturday":
case "Sunday":
echo "It's the weekend!";
break;
default:
echo "Just another weekday.";
}
// Output: Start of the work week!