1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help with switch()...

Discussion in 'PHP' started by chuckd1356, Mar 15, 2007.

  1. #1
    So here's what I'm trying to do.

    
    switch($_GET['fuseaction']) {
    case'add' :
    /* ... */
    break;
    /*...*/
    
    }
    
    PHP:
    So if the fuseaction corresponding case doesn't exist, the script will die.

    Please help!
     
    chuckd1356, Mar 15, 2007 IP
  2. tinkerbox

    tinkerbox Peon

    Messages:
    55
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    use default
    
    switch($_GET['fuseaction']) {
    case'add' :
    /* ... */
    break;
    
    default:
       /*...*/
    }
    
    PHP:
    also same if you use if else, like this:
    
    if ($_GET['fuseaction'] == "add") {
      /* ... */
    } elseif ($_GET['fuseaction'] == "insert") {
      /* ... */
    } else {
      /* ... */
    }
    
    PHP:
    more info check here:
    http://www.php.net/manual/en/control-structures.switch.php
    i like to use if else than switch :)
     
    tinkerbox, Mar 15, 2007 IP
  3. ArtInt

    ArtInt Active Member

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #3
    default is preferable if fuseaction can take several values, other than if ... elseif ... else
     
    ArtInt, Mar 16, 2007 IP
  4. chuckd1356

    chuckd1356 Active Member

    Messages:
    770
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    70
    #4
    I am using default already. But I want to kill the script if fuseaction takes a value that doesn't have a corresponding case.

    Do you know what I mean?
    
    ...
    // user typed example.com/page.php?fuse=foo
    
    $caseValue = $_GET['fuse'];
    
    case 'blah' :
    // ...
    break;
    
    case'new' :
    // ...
    
    default :
    
    
    break;
    
    // ...
    
    PHP:
    Since there was no case'foo' I want to throw an error
     
    chuckd1356, Mar 16, 2007 IP
  5. hemolack

    hemolack Guest

    Messages:
    31
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    In that case, don't just break the conditional, kill the script with die():
    
    ...
    // user typed example.com/page.php?fuse=foo
    
    $caseValue = $_GET['fuse'];
    
    case 'blah' :
    // ...
    break;
    
    case'new' :
    // ...
    
    default :
    die("I don't know how to handle $fuseaction\n");
    
    // ...
    
    PHP:
     
    hemolack, Mar 16, 2007 IP
  6. chuckd1356

    chuckd1356 Active Member

    Messages:
    770
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    70
    #6
    I guess that will work. I didn't want to use it in default though. I was gonna use that for something else.
     
    chuckd1356, Mar 16, 2007 IP