|PHP Control Structures 2 - switch and elseif|
|Monday, 15 February 2010|
Page 1 of 3
In most cases the if or the if-else is enough to build a program. A basic if-else statement selects between two alternatives and you can put these together to select between as many alternatives as you like. In this sense you really don't need anything more but to make programming easier PHP provides a number of additional instructions that make multiway selections easier - the switch and the elseif. Both are easy enough to understand but they both are sufficiently flexible and powerful to be easily misused.
More than two ways – the switch
As we saw in the first article on PHP control structures, sometimes you have to use if statements in combination to make things work correctly. A simple if can be thought of as a one-way selection – do this if the condition is true:
An if-else can be thought of as a two-way selection – do this if the condition is true and do that if the condition is false.
You can see that by using multiple if statements you can construct any selection of actions based on a set of conditions you care to think up.
For example you can use a set of sequential ifs to select one out of a number of possible things to do:
You can see that only one of the conditions can be true (assuming that $a isn't changed by any of the instructions that are part of the if) and so only one of the set of instructions will be obeyed.
If you trace the flow of control you will discover that there are four separate paths though the instructions depending on the value stored in $a.
A cleaned up version of the shape of this flow of control can be seen below:
The flow of control in an n-way split
This sort of multiple selection based on the value of a single variable or expression is so common that PHP and most other languages provide a special instruction that makes it easier to write and easier to understand.
The switch statement really can be thought of as a switch determining which instruction is executed.
The switch always takes the same form:
The list of cases is made up of a set of specifications and instructions:
The instructions following the case are only carried out if the switch expression is equal to the value. The break marks the end of the case.
For example, our previous list of if statements are equivalent to the following switch:
The switch statement is obeyed by first evaluating the switch expression, $a in this case. The value is then compared to each of the case values in turn. If it matches a case value then the instructions following the case are obeyed and when the break is executed control passes to the instruction following the switch's closing curly bracket. That is the break really can be though of as breaking out of the entire switch statement and moving on to what follows.
That is one and only one case and its list of instructions will be carried out according to the value of the switch expression. For this reason you should switch when only one of a number of possibilities apply.
Notice also that the switch expression can be complicated but everything works as an expression always evaluates to a single value which is then used to select which of the case blocks is obeyed. For example, the expression and the values can be a string:
Notice that this is just a two-way selection and so could just as easily be written as two if statements or an if-else. Which form of expression you choose is a matter of taste but you need to be aware that the if-else form is slightly trickier than it first seems.
|Last Updated ( Tuesday, 03 August 2010 )|