The Switch Statement If you want to refresh your memory of that last fateful article, point your browser here. Go ahead, I'll wait. I remember as a young child, my grandmother would make me go and get a switch from a tree every time I was bad; she would then try to whip some sense into me. And thank God she did. I mean, if she hadn't I wouldn't be the Pulitzer Prize, Nobel Laureate winning, oodles-of-fame-and-fortune, Hollywood-wheelin-and-dealin type of writer you see before you today. However, because of that experience, the word switch makes me...well...cringe (I was gonna say twitch, but that would have been horrible). Fortunately for me, I encountered my dearest friend, the Switch Statement, and now all that is in the past. The Switch Statement saves us fat and lazy programmers time and coding. Instead of writing a billion lines of If statements, we can use this bad boy instead. But enough yapping about it. Let's see it in action: <html> <body> <?php $wrestler = “Junkyard Dog”; echo “My favorite wrestler is $wrestler<br />”; switch ($wrestler) { case “Hulk Hogan”: echo “Whatcha gonna do when Hulkamania runs wild on you brother?!?”; break; case “Big John Stud”: echo “I'm Big. I'm John. And I'm a stud.”; break; case “Macho Man”: echo “Ewww YEAH! THE...MACH...O...MAN...RANDY SAVAGE and the Beautiful Missus E...liza...beth!”; break; case “Junkyard Dog”: echo “I wear pants that say THUMP on the buttocks.”; break; } ?> The above code creates a variable named $wrestler and stores the value “Junkyard Dog” in it. Next it prints the line “My favorite wrestler is” and places whatever is stored in the $wrestler variable into the sentence. Finally, it uses the Switch Statement to print a response based on the data inside the $wrestler variable. Since we already put the value “Junkyard Dog” in the variable, it will print: I wear pants that say THUMP on the buttocks. If the value were, say, “Hulk Hogan”, it would have printed the following to the screen: Whatcha gonna do when Hulkamania runs wild on you brother?!? And finally, if the value of $wrestler had been anything but the values listed, nothing would have occurred. But let's say you wanted something to happen if none of the values matched. For that, we could insert a Default clause, like so: <html> <body> <?php $wrestler = “Ravishing Rick Rude and Hillbilly Jim”; echo “My favorite wrestler is $wrestler<br />”; switch ($wrestler) { case “Hulk Hogan”: echo “Whatcha gonna do when Hulkamania runs wild on you brother?!?”; break; case “Big John Stud”: echo “I'm Big. I'm John. And I'm a stud.”; break; case “Macho Man”: echo “Ewww YEAH! THE...MACH...O...MAN...RANDY SAVAGE and the Beautiful Missus E...liza...beth!”; break; case “Junkyard Dog”: echo “I wear pants that say THUMP on the buttocks.”; break; default: echo “I don't even watch wrestling. Unless it involves females, midgets, or old men.”; break; } ?> All we did in this code is add the Default clause, and change the value of $wrestler so it doesn't match any of the values. Now if we ran this program it would print: I don't even watch wrestling. Unless it involves females, midgets, or old men.
blog comments powered by Disqus |
|
|
|
|
|
|
|