SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2000
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need to make a selection out of 23 options and have the script follow a completely different way for each. I wouldn't like to divide it into 23 different scripts since it would be extremely difficult to manage. Is there a way of doing this without using if clauses.

    i.e.
    I need an alternative to the following piece of code

    if $selection == 1 { ..... }
    if $selection == 2 { ..... }
    if $selection == 3 { ..... }
    ...
    ...
    ...
    if $selection == 23 { ..... }

    thank you

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    from http://www.php.net/manual/en/control...res.switch.php
    The switch statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for.

    The following two examples are two different ways to write the same thing, one using a series of if statements, and the other using the switch statement:
    Code:
    if ($i == 0) {
        print "i equals 0";
    }
    if ($i == 1) {
        print "i equals 1";
    }
    if ($i == 2) {
        print "i equals 2";
    }
     
    switch ($i) {
        case 0:
            print "i equals 0";
            break;
        case 1:
            print "i equals 1";
            break;
        case 2:
            print "i equals 2";
            break;
    }
    I suggest reading the whole chapter.

    In your case, under each "case" statement you would use include to include the file you need. eg,
    Code:
    switch($shape) {
       case "circle" :
          include("circle.php");
          break;
       case "square" :
          include("square.php");
          break;
       default :
          include("default.php");
    }

  3. #3
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or just to be picky :

    PHP Code:
    switch($shape) {
       case 
    "circle" :
          
    $inc "circle.php";
          break;
       case 
    "square" :
          
    $inc "square.php";
          break;
       default :
          
    $inc "default.php";
    }

    include 
    $inc
    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.

  4. #4
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good coding style though
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •