Home arrow PHP arrow PHP: The Switch Statement and Arrays

PHP: The Switch Statement and Arrays

In our last exciting adventure (back in early November), we braved crocodiles, ravenous editors, most of the PHP statements, and beginning loops. In this edition we'll cover the final statement, the Switch, and discuss arrays. So sit back, order your R2D2 robot to bring you a cold, frosty Jolt Cola, and let's get cracking.

TABLE OF CONTENTS:
  1. PHP: The Switch Statement and Arrays
  2. The Variable's Big Brother
  3. Printing the Array Values
  4. Associative Arrays
  5. The Multidimensional Array
  6. Beyond Multidimensional Arrays
By: James Payne
Rating: starstarstarstarstar / 6
January 07, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By James Payne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: