To be continued,the switch-case statement comes.
An alternative to the if -elseif-else statement is the switch-case statement,which does almost the same thing:it tests a variable against a series of values until if finds a match,and then executes the code corresponding to that match.Consider the following code listing,which is equivalent to the preceding one:
<?php
//handle multiple possibilities
//define a different message for each day
$today='Tuesday';
switch($today){
case 'Monday':
echo 'Monday/'s child is fair of face.';
break;
case 'Tuesday':
echo 'Tuesday/'s child is full of grace.';
break;
case 'Wednesday':
echo 'Wednesday/'s child is full of woe.';
break;
case '':......
default:
echo 'No information available for that day';
break;
}
?>
--------------------------------------------------------------------------------
The switch-case construct differs from the if-elseif-else construct in one important way.Once PHP finds a case statement that evaluates to true,it executes not only the code corresponding to that case statement,but also the code for all subsequent case statements.If this is not what you want,add a break statement to the end of each case block(as is done in the previous listing)to tell PHP to break out of the switch-case statement block once it executes the code corresponding to the first true case.
Notice also the 'default' case:as the name suggests,this specifies the default set of actions PHP should take if none of the other cases evaluate to true.This default case,like the else branch of the if-elseif-else block,is very useful as a "catch-all" handler for unforeseen situations.
An alternative to the if -elseif-else statement is the switch-case statement,which does almost the same thing:it tests a variable against a series of values until if finds a match,and then executes the code corresponding to that match.Consider the following code listing,which is equivalent to the preceding one:
<?php
//handle multiple possibilities
//define a different message for each day
$today='Tuesday';
switch($today){
case 'Monday':
echo 'Monday/'s child is fair of face.';
break;
case 'Tuesday':
echo 'Tuesday/'s child is full of grace.';
break;
case 'Wednesday':
echo 'Wednesday/'s child is full of woe.';
break;
case '':......
default:
echo 'No information available for that day';
break;
}
?>
--------------------------------------------------------------------------------
The switch-case construct differs from the if-elseif-else construct in one important way.Once PHP finds a case statement that evaluates to true,it executes not only the code corresponding to that case statement,but also the code for all subsequent case statements.If this is not what you want,add a break statement to the end of each case block(as is done in the previous listing)to tell PHP to break out of the switch-case statement block once it executes the code corresponding to the first true case.
Notice also the 'default' case:as the name suggests,this specifies the default set of actions PHP should take if none of the other cases evaluate to true.This default case,like the else branch of the if-elseif-else block,is very useful as a "catch-all" handler for unforeseen situations.