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.

About Switch Statement (php)

Discussion in 'PHP' started by Nasimov, Apr 28, 2004.

  1. #1
    It's possible to have multiple values in one case?

    case "1,2,3" 
    PHP:
    Thanks.
     
    Nasimov, Apr 28, 2004 IP
  2. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #2
    No, you can't use boolean logic within the case function.

    If you could, it probably would be done as:
    case (1 || 2 || 3):
    PHP:
    But you can't, so... :)

    - Shawn
     
    digitalpoint, Apr 28, 2004 IP
  3. er2er

    er2er Peon

    Messages:
    9
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    However, you can try:
    case 1:
    case 2:
    case 3: (do something); break;
    PHP:
     
    er2er, Apr 29, 2004 IP
  4. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #4
    I don't think that would work. As something that passes the case 1: operator will not pass the case 3: operator (and that's where it needs to get into since that's where the code is.

    - Shawn
     
    digitalpoint, Apr 29, 2004 IP
  5. Owlcroft

    Owlcroft Peon

    Messages:
    645
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #5
    As a nonexpert, I feel I'm missing something there. Since the first two case statement lack a break; , wouldn't the code just fall through to case 3?

    The online PHP manual says at switch:

    Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. If you don't write a break statement at the end of a case's statement list, PHP will go on executing the statements of the following case. [emphases added]

    ?
     
    Owlcroft, Apr 29, 2004 IP
  6. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #6
    The number after the case is not an execution order, rather a variable must match that in order for whatever is after it to be executed. So if it equals 1, it would not get through to the actual code.

    - Shawn
     
    digitalpoint, Apr 29, 2004 IP
  7. Owlcroft

    Owlcroft Peon

    Messages:
    645
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Sorry to be obtuse, but I'm still not following that.

    $trythis=1;
    switch ($trythis)
    {
    case 1:
    case 2:
    case 3:
    echo 'Variable TRYTHIS is '.$trythis;
    break;
    case 4:
    default:
    echo 'Variable TRYTHIS not equal to 1, 2, 3 or 4';
    break;
    }

    Would not we see the message 'Variable TRYTHIS is 1"?

    In fact, I just tried that very script, and got that readout.
     
    Owlcroft, May 1, 2004 IP
    digitalpoint likes this.
  8. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #8
    Hmmm... well if it works for you, then I guess it works. I never tried it myself, but seems strange to me it would work. But cool.

    - Shawn
     
    digitalpoint, May 1, 2004 IP
  9. Owlcroft

    Owlcroft Peon

    Messages:
    645
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I hadn't thought about it before, but in retrospect I'm guessing that it was in fact designed to work that way, to accomodate just that situation. My reasoning is based on the reason it works, namely the need for a break statement to stop code processing in a matched case statement. In the abstract, that seems a silly way to do things, requiring numerous breaks; I have often thought, while typing them in, that it would be much neater, and more obvious, to have another case statement, or the end of the switch structure, automatically terminate processing, kind of like a new <tr> in HTML terminates the old row (or did till XHTML).

    But I see now that it makes sense by allowing multiple cases to all use the same code steps without the need to repeat those steps for every case: you just place your ORed cases one after another.

    (But from my own meager experience, that is less useful than automatic termination, and I still wish they had done it the other way.)
     
    Owlcroft, May 1, 2004 IP
  10. er2er

    er2er Peon

    Messages:
    9
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    The switch-case construct in PHP works as in C++.
    After a switch, code execution starts at the first matching case (or at a default one, or even after the switch statement if no case matches). Then it continues until the first case.
    It's a bit tricky, but C++ (C, C#, PHP,...) programmers have to get used to it :)
     
    er2er, May 4, 2004 IP
  11. hexed

    hexed Peon

    Messages:
    333
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #11
    case 1: case 2: case 3: $n = ""; break;

    works perfectly fine.
     
    hexed, May 4, 2004 IP
  12. freesmegger

    freesmegger Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    be careful not to forget the break statements;

    
    switch ($thistest)
    {
    case 1:
    print "thistest is one<BR>";
    case 2:
    print "thistest is two<BR>";
    default:
    print "thistest is neither one or two";
    break;
    }
    
    Code (markup):

    if the '$thistest' variable is 1, the output you get is;

    thistest is one
    thistest is two
    thistest is neither one or two
    Code (markup):
    if the '$thistest' variable is 2, the output you get is;

    thistest is two
    thistest is neither one or two
    Code (markup):
     
    freesmegger, Oct 27, 2005 IP