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
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
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] ?
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
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.
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
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.)
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
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):