Loans - The eBay Song - Online Advertising - Loan - Myspace Layouts

PDA

View Full Version : About Switch Statement (php)


Nasimov
Apr 28th 2004, 9:33 am
It's possible to have multiple values in one case?

case "1,2,3"

Thanks.

digitalpoint
Apr 28th 2004, 10:07 am
No, you can't use boolean logic within the case function.

If you could, it probably would be done as:
case (1 || 2 || 3):

But you can't, so... :)

- Shawn

er2er
Apr 29th 2004, 9:20 am
However, you can try:
case 1:
case 2:
case 3: (do something); break;

digitalpoint
Apr 29th 2004, 9:28 am
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

Owlcroft
Apr 29th 2004, 10:59 pm
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.
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]

?

digitalpoint
Apr 29th 2004, 11:03 pm
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

Owlcroft
May 1st 2004, 1:37 am
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.

digitalpoint
May 1st 2004, 8:10 am
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

Owlcroft
May 1st 2004, 1:24 pm
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.)

er2er
May 4th 2004, 9:59 am
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 :)

hexed
May 4th 2004, 11:54 pm
case 1: case 2: case 3: $n = ""; break;

works perfectly fine.

freesmegger
Oct 27th 2005, 2:18 pm
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;
}



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

thistest is one
thistest is two
thistest is neither one or two

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

thistest is two
thistest is neither one or two