This is a basic PHP program to describe the switch case with an example and real-time output.
Below examples will help you in the better understanding of Switch case in PHP programming language. Switch case statement works like the if-else-if statement in PHP programming language.
Below is the standard syntax of switch case statement in PHP programming language.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
switch(expression) { case value1: //do something here break; case value2: //do something here break; case value3: //do something here break; .......... default: //default part when any case is not matched } |
In the below program, I am going to share a PHP program to print the day name based on the entered day number. Copy the below code and execute it with the help of PHP compiler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php $num=2; switch($num){ case 1: echo("Today is Monday"); break; case 2: echo("Today is Tuesday"); break; case 3: echo("Today is Wednesday"); break; case 4: echo("Today is Thursday"); case 5: echo("Today is Friday"); case 6: echo("Today is Satarday"); case 7: echo("Today is Sunday"); default: echo("You have entered a wrong number. Please enter a mumber between 1-7."); } ?> |
#First try
Today is Tuesday
#Second try to check default case change the $num values as 8
You have entered a wrong number. Please enter a mumber between 1-7.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.