Using logic (boolean) operators inside a "switch"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gxpark
Forum Commoner
Posts: 33
Joined: Fri Jun 21, 2002 4:01 pm
Location: Mexico City

Using logic (boolean) operators inside a "switch"

Post 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.
will
Forum Contributor
Posts: 120
Joined: Fri Jun 21, 2002 9:38 am
Location: Memphis, TN

Post 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;
}
gxpark
Forum Commoner
Posts: 33
Joined: Fri Jun 21, 2002 4:01 pm
Location: Mexico City

Post by gxpark »

Oh yes, I forgot I read that in the manual. Thanks for reminding me! :D
Post Reply