PHP Switch
In PHP, a switch statement is a control structure used for making decisions based on the value of a given expression. It provides an alternative way to handle multiple conditions and execute different code blocks based on the value of a single expression.
Syntax
switch (expression) {
case value1:
// Code to execute if expression equals value1
break;
case value2:
// Code to execute if expression equals value2
break;
// Additional cases as needed
default:
// Code to execute if expression doesn't match any case
}
Example
<?php
$day = "sunday";
switch ($day) {
case "Monday":
echo "It's Monday!";
break;
case "Tuesday":
echo "It's Tuesday!";
break;
default:
echo "It's not Monday or Tuesday.";
}
?>
// output it's not Monday or Tuesday.