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

 

No break!

Now we come to a complication that you should skip if this is the first time you have encountered the switch statement or anything like it.

Banner

If you simply use the switch as described then its easy to use and understand and most programmers advice is to stick to this. However the break that ends each case is optional. If you leave out then the flow of control passes to the next case below and so on until either it reaches a break or the end of the switch statement. For example:

 switch ($a){
case 1:
instructions1;
case 2:
instructions2;
break;
}

If $a is 1 then instructions1 are carried out but without a break at the end of case 1 control falls through to the second case and instructions2 are also carried out. If $a is 2 then only case 2 is obeyed.

You should, of course, never leave out the break by accident. The result is code that doesn't do what you expect and this can be very difficult to find as programmers some times tend to read a "break" were no break exists simply from habit.

It is also sometimes tempting to leave out a break statement because it seems to fit in with what you want the code to do. For example:

 switch($gender)
case "female":
echo "Don't forget to
visit our sister site!";
case "male":
echo "Welcome to a new member.";
break;
}

This adds a special message for new female members of the site but by not including a break makes sure that they also get the same message that a male gets. This saves having to add the default message to the female case but it also makes the code more fragile. Notice that the order of the case statements now matter if you swap the order of the male and female case statements the code no longer works. The same thing happens if you come back at a later date and add another case statement between the male and female case statements - admittedly not likely in this example but in general it often happens.

The key idea is that when you make use of fortuitous relationships between cases by leaving out the break you create a switch that is more fragile in the sense that it is sensitive to small changes in the code that switch statements using break tolerate. That is the order of the case statements now matter. Fragile code is something best avoided if there is an alternative so it is recommended that you always use break.

else is hard

When you write an if statement then it is usually very easy to work out when the conditional instructions are obeyed. For example:

 if($a>0){
echo("$a is positive);
}

then it is easy to say when the echo statement will be obeyed - when the condition is true and this translates to when the value stored in $a is greater than zero. Now consider the slightly modified example with an else clause:

 if($a>0){
echo("$a is positive);
}else{
echo("$a isn't positive);
}

Now you can see quite clearly that the echo that is in the else part of the statement is only obeyed when the condition is false or when the "not" of the condition is true - and programmers are usually not very good at working out when a condition is false or what the "not" of a condition is true.

For example, in this case the else is obeyed when $a>0 is not true. That is when $a is 0 or less than zero. That is the "not" or logical converse of $a>1 is $a<=1. Most programmers would find this fairly obvious but it isn't trivial and it gets much more complicated very quickly.

Even in very simple situations the psychology of a condition can cause a programmer to make an error. For example,

 if($gender=="male"){
echo("welcome sir");
}else{
echo("welcome madam");
}

The negation of the condition $gender=="male" is $gender!="male" which translates to if $genders is any other value than "male" then the else part of the statement is obeyed. Many novice programmers will assume that the negation of $gender=="male" is $gender=="female" because that's the way we think about it in the real world.

If you think that the above examples are trivial and you would never make a mistake with an else then perhaps a more advanced example will illustrate that "else really is hard". You can combine conditions using the "and" and "or" operators in a way that fits in with their normal meaning. For example, consider the following if statement:

 $a=15;
if(($a>10) and ($a<20)){
echo("the value is
between 10 and 20");
}else{
echo("the value isn't
between 10 and 20");
}

Clearly first echo is obeyed if $a is greater than 10 and less than 20, i.e. both conditions have to be true. The else is obeyed when the whole condition works out to be false, i.e. when $a isn't between 10 and 20.

Now can you write down the condition that is true when the else is obeyed?

It's the negation of (($a>10) and ($a<20))
which works out to be ($a<=10) or ($a>=20).

That is, the else is obeyed when $a is less than or equal to 10 or greater than or equal to 20. The surprise is in the change from and to or in addition to the change to the inequalities.

Banner

<ASIN:032152599X>

<ASIN:1590598628>

<ASIN:0596006810>

<ASIN:1590597311>



Last Updated ( Tuesday, 03 August 2010 )