Switch Case In PHP With Multiple Case Example
Last Updated : Mar 11, 2024
In this tutorial we will show you the solution of switch case in PHP with multiple case example, as we know that switch is used in php to select one of many block of codes to be executed.
In our last article, we understand that what is switch and how to use switch in php.
This article is also related to switch statement, but in this article we understand that how to use switch statement in php with multiple cases with example. So let us see how to do this.
Step By Step Guide On Switch Case In PHP With Multiple Case Example :-
Here, as we know that switch statement is used where we have multiple number of conditions and we want to execute specific block of statements if condition become true.
But what if we want to execute a specific block of statement with multiple cases, then how to do this.
We hope the code given below may help you in this condition, after this codes we will understand what we have done in these.
// simple switch statement in php <?php switch ($i) { case "Monday": echo "i is monday"; break; case "tuesday": echo "i is tuesday"; break; case "wednesday": echo "i is wednesday"; break; case "thursday": echo "i is thursday"; break; case "friday": echo "i is friday"; break; case "saturday": echo "i is saturday"; break; default: echo "i is sunday"; } ?> // switch statement in php with multiple cases <?php switch ($i) { case 1: case 2: echo "i is less than 3"; break; case 3: echo "i is 3"; break; case 4: echo "i is 4"; break; default: echo "i is equal to or greater than 5"; } ?>
- Here, first in this article we will show you a simple example that how to use switch statement in php. As you see we use here example of Days of the week.
- When user enters something which matches with our cases then it will display the specific output.
- If value enter by user doesn’t matches with our cases then the value inside default executed. We hope you easily understand about switch statement and how it works.
- Now, in next case we will show you a couple of lines of code in php. In which we again get value of i and when it matches with our cases then it will show the required output as above.
- But one thing here to notice is that here we use multiple cases for a single block of statements.
- It means that, from above example if our case is either 1 or 2 it will execute the same block that is given after case 2.
- So, we hope you understand above codes easily with the help of these steps and we hope this article may help you.
Conclusion :-
At last in conclusion, here we can say that with the help of this article we are able to understand how to use switch case in php with multiple cases.
I hope this tutorial on switch case in PHP with multiple case example helps you and the steps and method mentioned above are easy to follow and implement.