PHP Switch
The switch statement is an efficient alternative to multiple if-elseif branches.
Syntax
switch ($day) {
case "Monday":
echo "Start of week";
break;
case "Friday":
echo "Weekend near";
break;
case "Saturday":
case "Sunday":
echo "Weekend!";
break;
default:
echo "Midweek";
}
Key Points
- Use
breakto prevent fall-through defaulthandles unmatched values- Use
match(PHP 8+) for strict comparison with return values