A Tour of Decision Making Structures in PHP - SWITCH-CASE statements
(Page 4 of 5 )
As the ?: is an alternative to if-then statements, a switch-case statement provides a cleaner way to code lengthy if-elseif statements where each test checks the same variable.
<?php $weather = "rain"; echo "The weather forecast calls for $weather. "; switch ($weather) { case "snow": echo "You'd better bundle up!<br />"; break;
case "rain": echo "Be sure to bring an umbrella!<br />"; break;
case "wind": echo "Take the day off and fly a kite!<br />"; break;
default: echo "Who knows what to expect?<br />"; break; } ?> |
The comparisons made between the variable in the switch and the values designated by the case keyword. The code block of each case is terminated with break. A default case can be used to designate a piece of code that will execute if no other cases return a true comparison.
Next: Conclusion >>
More Programming Basics Articles
More By bluephoenix