...

View Full Version : Switch/Case ... Is this legal?



markhartnady
05-06-2004, 03:08 PM
Is this permitted in any version of PHP?


switch ($var) {
case 1: ...code...; break;
case 2: ...code...; break;
case 3,4,5,6: ...code...; break;
default: code;
}

In particular I am refering to case 3,4,5: (numbers seperated by a comma).

If not, if there another way around this?

firepages
05-06-2004, 03:50 PM
not legal no but you can do this ...



<?
switch (true) {
case $var == 1 : echo '1'; break;
case $var == 2 : echo '2'; break;
case $var >0 && $var < 6 : echo '3 - 6'; break;
default: echo '>6 or 0';
}
?>

raf
05-06-2004, 07:55 PM
Nice one :thumbsup: . Never seen that before and to be honest an if -elseif-elseif-else construct would do the same.

But it's a nice trick.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum