In PHP, the switch statement is a control structure that provides an easy way to execute different blocks of code depending on the value of a variable. It allows you to test the value of an expression against multiple cases and execute different code blocks depending on which case matches the value of the expression. In this blog, we will explore the switch statement in PHP, how it works, and provide examples of how to use it.
What is a Switch Statement?
A switch statement in PHP is a control structure that allows you to test the value of an expression against multiple cases and execute different code blocks depending on which case matches the value of the expression. The basic syntax for a switch statement in PHP is as follows:
switch (expression) {
case value1:
// code to be executed if expression equals value1
break;
case value2:
// code to be executed if expression equals value2
break;
...
default:
// code to be executed if none of the cases match
break;
}
The switch statement evaluates the expression and compares it to each case until it finds a match. When a match is found, the code block associated with that case is executed. If no match is found, the code block associated with the default case is executed (if there is one).
How Does it work?
Let’s look at an example to understand how a switch statement works in PHP. Consider the following code:
$day = "Monday";
switch ($day) {
case "Monday":
echo "Today is Monday";
break;
case "Tuesday":
echo "Today is Tuesday";
break;
case "Wednesday":
echo "Today is Wednesday";
break;
default:
echo "Today is not a weekday";
break;
}
In this code, we define a variable $day
with the value “Monday”. Then, we define a switch statement with the expression $day
. The code inside each case block prints a message depending on the value of $day
. If the expression does not match any of the cases, the code block associated with the default case is executed.
The output of this code will be:
Today is Monday
As you can see, the switch statement evaluated the expression $day
and found a match with the case “Monday”, so the code block associated with that case was executed.
Another example of a switch statement in PHP could be to test the value of user input. Consider the following code:
$user_input = "red";
switch ($user_input) {
case "red":
case "green":
case "blue":
echo "Your favorite color is a primary color";
break;
default:
echo "Your favorite color is not a primary color";
break;
}
In this code, we define a variable $user_input
with the value “red”. Then, we define a switch statement with the expression $user_input
. The code inside the first case block will be executed if the expression matches “red”, “green”, or “blue”. If the expression does not match any of those cases, the code block associated with the default case is executed.
The output of this code will be:
Your favorite color is a primary color
As you can see, the switch statement evaluated the expression $user_input
and found a match with the case “red”, so the code block associated with that case was executed.