A Php program to execute the switch statement. Using this program we can execute the single data type element for multiple times. The switch-case statement is a substitute for the if-elseif-else statement, which accomplishes the same goal. The switch-case statement compares a variable to a set of values until it finds one that matches, and then executes the code block that corresponds to that match.
$favfruit = "apple";
echo "Your favorite fruit is apple";
echo "Your favorite fruit is orange";
echo "Your favorite fruit is strawberry";
echo "None of the fruits is selected";
Your favorite fruit is apple
What are uses of switch case statement in php
The value stored in variable num is compared using a switch construct, and the block of statements associated with the matching case is executed. Use a switch whenever you have more than two conditions on a single variable; for example, if each weekday has a different action, you should use a switch. Ifs should be used in other circumstances with numerous variables or sophisticated if clauses, but there is no set guideline for when to use them.
What are advantages and disadvantages of switch case in php
The key benefit is that the user may compare a large number of variable values with a single switch statement and a lot of cases. Because the program is divided into modules by these instances, it is easy to spot errors. When a large number of values for a variable must be compared, it is commonly utilised.
The float constant cannot be used in both the switch and the case. In this scenario, you cannot use the variable expression. In two separate circumstances, you can’t utilise the same constant. In this scenario, we are unable to use the relational expression.