Introduction
Switch is another language construct that is used to control program flow. The switch construct takes a single parameter and compares it for equality to a set of cases that have been specified.
Example Scenerio
Let’s take an take the classic case where we have seven days, represented by the numbers from zero to six.
We are going to pass a variable holding a number to our switch, the switch then looks for the case where the parameter it is passed is equal to a case. The parser then executes the code after the matching case. The parser continues to execute code until the end of the switch or a break statement is reached.
What is a “break”?
The break is used to tell the parser to break or stop executing the current block of code. A break statement is required to indicate the end of a block inside a switch, much like the curly brackets are used to indicate the start and end of a block after an if statement.
Implementing a Switch
One Input to One Block
The script below echoes the name of a day depending on the value of our variable $day. In this scenario, there is one output for a single input.
|
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 $day = 4; switch ($day){ case 0 : echo 'Sunday'; break; case 1 : echo 'Monday'; break; case 2 : echo 'Tuesday'; break; case 3 : echo 'Wednesday'; break; case 4 : echo 'Thursday'; break; case 5 : echo 'Friday'; break; case 6 : echo 'Saturday'; break; } ?> |
Multiple Input to a Single Block
Inside a switch we are also able to have multiple possible input values with a single output. To do this you simply group a number of cases one after another and follow with an appropriate block of code. For example the script below tells you if the number is even or odd.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php $day = 4; switch ($day){ case 0 : case 2 : case 4 : case 6 : case 8 : case 10 : echo 'Even!'; break; case 1 : case 3 : case 5 : case 6 : case 7 : case 11 : echo 'Odd!'; break; } ?> |
The Undefined Case
If for any reason the value of the variable being compared did not have a case defined, no code inside the switch would execute since a case for that particular value is not defined. We have the option to define a default case that will be executed if the value of parameter being compared is not found. We define the default case with the default keyword in place of the case and the value.
In the example below we have grouped the values from zero to six so that they execute the code echoing, “Its a valid day from 0 to 6!”. If the parameter passed to the switch has any other value it will display the code defined after the default case.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php $day = 10; switch ($day) { case 0 : case 1 : case 2 : case 3 : case 4 : case 5 : case 6 : echo 'Its a valid day from 0 to 6!'; break; default : echo 'Not a valid number for a day!'; break; } ?> |
Note about the “break”
The break tells the parser to stop executing the current block of code. If you happen to omit the break from your cases inside the switch, the parser will execute all of the code starting at the matching case. Leaving the break out can result in unexpected output, remember to use the break to indicate the end of a cases’ block of code.
Switch vs If-Else
You may have noticed some similarities between the switch and if-else blocks. Both of these constructs act in a similar manner and they can both almost be used interchangeably. The decision on which to use is yours.
Comments, Questions or Concerns
If you have any comments, questions or concerns feel free to voice them on this site or contact us directly at php@htmlandphp.com