Page 1 of 1

Switch Statement: Multiple Values

Posted: Mon Nov 07, 2005 12:23 am
by Hyarion
I've been learning php for a while now and really enjoying it. The only thing I would like to know is how to use (if possible) multiple values for a switch statement. I'm used to Visual Basic's Selelect statement. In VB I could do this:

Code: Select all

Select var$
case is < 20
'do something

case 20
'do something specific

case is > 20
'do something else

end select
How would one do this in PHP?

As far as I can see one would either need to use IF...ELSEIF statements or do something like this:

Code: Select all

switch ($var){
case 0:
case 1:
case 2:
...
case 19
//do something

case 20:
//do something specific

case 21:
case 22:
case 23:
...
case 30:
//do something else
}
This seems like a bit of a time consumer.

Is there a way to do this properly in PHP?

Posted: Mon Nov 07, 2005 12:28 am
by feyd
php allows full expressions in the case condition...

Posted: Mon Nov 07, 2005 12:33 am
by Hyarion
ok, how does one use the expressions?

Code: Select all

switch($var){
case > 20:
 //does not work in my testing.

case $var > 20:
//havn't tested, is this how one uses it?

}

Posted: Mon Nov 07, 2005 12:37 am
by feyd
although you should have tested it ;)

the second one.

Posted: Mon Nov 07, 2005 12:42 am
by Hyarion
Thank you. :wink: