Page 1 of 1

Using logic (boolean) operators inside a "switch"

Posted: Sat Jun 22, 2002 5:11 pm
by gxpark
Hi,

Is it somehow possible to specify several options for a single case inside a switch block? Like this (which doesn't work):

Code: Select all

switch ($something) {
    case $this or $thisone or $eventhis:
        //do something
    default:
        //do other stuff
}
I hope I don't need to write an individual case for each option...

Thanks in advance.

Posted: Sat Jun 22, 2002 5:27 pm
by will
yes... a case statement will slip down to the next one until there is a break statement, so this would do the trick....

Code: Select all

switch($test)
{
    case'a':
    case'b':
    case'c':
        // do something for cases a-c
        break;
    case'd':
        // do something for case d
        break;
}

Posted: Sat Jun 22, 2002 5:32 pm
by gxpark
Oh yes, I forgot I read that in the manual. Thanks for reminding me! :D