PHP
  Home arrow PHP arrow PHP: The Switch Statement and Arrays
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

PHP: The Switch Statement and Arrays
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2008-01-07

    Table of Contents:
  • PHP: The Switch Statement and Arrays
  • The Variable's Big Brother
  • Printing the Array Values
  • Associative Arrays
  • The Multidimensional Array
  • Beyond Multidimensional Arrays

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    PHP: The Switch Statement and Arrays
    (Page 1 of 6 )

    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.

    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


       · There is a mistake in codeinstead of $mygirlfriends[1] . “and” . $names[2]...
       · Hey thanks for catching that. They tie me up in a dungeon and force me to write...
     

       

    PHP ARTICLES

    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5
    - Working with Multiple Document Nodes with th...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway