PHP
  Home arrow PHP arrow PHP: The Switch Statement and Arrays
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 5
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT