switch is similar with multiple if structure.
Sometimes you want to compare same expression with different multiple values, and execute a different statements depending on value. This can be done using switch statement.
How switch work
A switch structure execute line by line. If the case value matches the value of switch expression then only the statements will execute.
In a switch statement, the condition is evaluated only once and the result is compared to each case statement.
Syntax of switch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php switch(condition) { case value1: statement1; break; case value2: statement2; break; case value3: statement3; break; default: statement N; } ?> |
Flowchart of switch
Example of switch –
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 28 29 30 31 32 33 |
<?php $m = 6; switch ($m) { case 1: echo "You born in January"; break; case 2: echo "You born in February"; break; case 3: echo "You born in March"; break; case 4: echo "You born in April"; break; case 5: echo "You born in May"; break; case 6: echo "You born in June"; break; case 7: echo "You born in July"; break; case 8: echo "You born in August"; break; case 9: echo "You born in September"; break; case 10: echo "You born in October"; break; case 11: echo "You born in November"; break; case 12: echo "You born in December"; break; default: echo "Please enter valid month number"; } ?> |
Output
1 |
You born in June |
PHP continues to execute the statements until the end of the switch block, or it sees a break statement.
If there is no break keyword at end of case’s statements, PHP will execute the statements of the following case.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php $m = 6; switch ($m) { case 1: echo "You born in January"; case 2: echo "You born in February"; case 3: echo "You born in March"; case 4: echo "You born in April"; case 5: echo "You born in May"; case 6: echo "You born in June"; case 7: echo "You born in July"; case 8: echo "You born in August"; case 9: echo "You born in September"; case 10: echo "You born in October"; case 11: echo "You born in November"; case 12: echo "You born in December"; default: echo "Please enter valid month number"; } ?> |
Output
1 2 3 4 5 6 7 8 |
You born in June You born in July You born in August You born in September You born in October You born in November You born in December Please enter valid month number |
Switch case can be a string. For example
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 28 29 30 31 32 33 34 35 36 |
<?php $d = date("D"); switch ($d){ case "Mon": echo "Today is Monday"; break; case "Tue": echo "Today is Tuesday"; break; case "Wed": echo "Today is Wednesday"; break; case "Thu": echo "Today is Thursday"; break; case "Fri": echo "Today is Friday"; break; case "Sat": echo "Today is Saturday"; break; case "Sun": echo "Today is Sunday"; break; default: echo "Enter valid day"; } ?> |
case statement can be empty, which simply passes control to next case statement.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $a = 3; switch($a) { case 0: case 1: case 2: echo "You choose 3"; break; case 3: echo "You choose 4"; break; } ?> |
Default case is a special case. If any case value wasn’t matched with switch expression result then default case will execute.