Skip to main content
Unlisted page
This page is unlisted. Search engines will not index it, and only users having a direct link can access it.

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 break to prevent fall-through
  • default handles unmatched values
  • Use match (PHP 8+) for strict comparison with return values