PHP Control Structures 2 - switch and elseif
Written by Administrator   
Monday, 15 February 2010
Article Index
PHP Control Structures 2 - switch and elseif
No break
The default

Banner

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:

 if($a==1){
echo "do this only if
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.

 if($a==1){
echo "do this only if
condition is true";
}else{
echo "do this only if
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:

 if($a==1){
echo "the value is 1";
}
if($a==2){
echo "the value is 2";
}
if($a==3){
echo "the value is 3";;
}
if($a==4){
echo "the value is 4";;
}

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:

nway

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:

 switch(expression)
{
list of cases
}

The list of cases is made up of a set of specifications and instructions:

 case value:
instructions;
break;

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:

 switch ($a){
case 1:
instructions1;
break;
case 2:
instructions2;
break;
case 3:
instructions3;
break;
case 4:
instructions4;
break;
}

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:

 switch($gender){
case "male":
echo "hello sir how can I help?";
break;
case "female":
echo "hello madam how can I help?";
break;
}

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.

Banner

<ASIN:0672329166>

<ASIN:0596157134>

<ASIN:1593271735>

<ASIN:1590599098>



Last Updated ( Tuesday, 03 August 2010 )