The body of a switch statement is known as a switch block. A statement in the switch block can be labelled with one or more case or default labels. The switch statement evaluates its expression, then executes all statements that follow the matching case label.
Syntax
switch (expression) {
case label1:
......
......
break;
case label2:
......
......
break;
case label3:
......
......
break;
...
default:
.....
}
Example
switch ($day) {
case 1:
echo "Sunday";
break;
case 2:
echo "Monday";
break;
case 3:
echo "Tueday";
break;
case 4:
echo "Wednesday";
break;
case 5:
echo "ThursDay";
break;
case 6:
echo "Friday";
break;
case 7:
echo "Saturday";
break;
default:
echo "You entered wrong day number";
}
Find the below working application.
switch_demo.php
#!/usr/bin/php
<?php
$day = 3;
switch ($day) {
case 1:
echo "Sunday";
break;
case 2:
echo "Monday";
break;
case 3:
echo "Tueday";
break;
case 4:
echo "Wednesday";
break;
case 5:
echo "ThursDay";
break;
case 6:
echo "Friday";
break;
case 7:
echo "Saturday";
break;
default:
echo "You entered wrong day number";
}
?>
Output
$./switch_demo.php
Tueday
Explanation
As you see in the above program, variable “day” set to the value 2. So in the switch case, case 2 is executed. If the day is set to 1, then case 1 will execute and come out of the switch.
You can see in the above program, each case has a break statement associates with it, is it necessary ? depends on the use case, you may use or may not.
For example, for Saturday and Sunday, I would like to print the message ‘Happy weekend!!!!’. You can combine both cases 1 and 7 like below.
switch ($day) {
case 1:
case 7:
echo "Happy weekend!!!!";
break;
.....
.....
}
Find the below working application.
switch_demo_1.php
#!/usr/bin/php
<?php
$day = 1;
switch ($day) {
case 1:
case 7:
echo "Happy weekend!!!!";
break;
case 2:
echo "Monday";
break;
case 3:
echo "Tueday";
break;
case 4:
echo "Wednesday";
break;
case 5:
echo "ThursDay";
break;
case 6:
echo "Friday";
break;
default:
echo "You entered wrong day number";
}
?>
Output
$./switch_demo_1.php Happy weekend!!!!
Can I use strings in switch statements?
Yes, you can use strings in switch statement.
switch_strings_demo.php
#!/usr/bin/php
<?php
$day = "one";
switch ($day) {
case 'one':
case 'seven':
echo "Happy weekend!!!!";
break;
case 'two':
echo "Monday";
break;
case 'three':
echo "Tueday";
break;
case 'four':
echo "Wednesday";
break;
case 'five':
echo "ThursDay";
break;
case 'six':
echo "Friday";
break;
default:
echo "Wrong input!!!!";
}
?>
Output
$./switch_strings_demo.php
Happy weekend!!!!
Alternative switch syntax
switch (expression) :
case label1:
......
......
break;
case label2:
......
......
break;
case label3:
......
......
break;
...
default:
.....
endswitch;
switch_alternative_syntax.php
#!/usr/bin/php
<?php
$day = "one";
switch ($day) :
case 'one':
case 'seven':
echo "Happy weekend!!!!";
break;
case 'two':
echo "Monday";
break;
case 'three':
echo "Tueday";
break;
case 'four':
echo "Wednesday";
break;
case 'five':
echo "ThursDay";
break;
case 'six':
echo "Friday";
break;
default:
echo "Wrong input!!!!";
endswitch;
?>
Output
$./switch_alternative_syntax.php
Happy weekend!!!!
Previous Next Home
No comments:
Post a Comment