Reputation: 40167
I'd like to pass multiple values in a single switch case. I realize its not possible they way I'm trying to do it. Is there another way, short of placing each case on its on line?
switch(get_option('my_template'))
{
case 'test1', 'test2':
return 850;
break;
default:
return 950;
}
Upvotes: 14
Views: 16374
Reputation:
Unless you use break;
on your case, execution just falls into the next case. You can use this to your advantage by stacking each of your cases together, e.g.:
switch(get_option('my_template')) {
case 'test1':
case 'test2':
return 850;
break;
default:
return 950;
}
Because there is no break;
on the 'test1' case, when execution ends on that case (i.e. immediately, since there is no logic in it), control will then fall to the 'test2' case, which will end at its break
statement.
In this case, the break
isn't even needed for these cases, since the return
statement will take care of breaking out of the switch
on its own.
Upvotes: 2
Reputation: 16768
Within the switch structure i dont believe there is any way of doing something like an 'or' on one line. this would be the simplest way:
switch(get_option('my_template'))
{
case 'test1':
case 'test2':
return 850; break;
default:
return 950;
}
But, especially if you are only returning a value and not executing code, i would reccomend doing the following:
$switchTable = array('test1' => 850, 'test2' => 850);
$get_opt = get_option('my_template');
if in_array($get_opt, $switchTable)
return $switchTable[$get_opt];
else return 950;
Upvotes: 2
Reputation: 5102
How about this
switch(get_option('my_template'))
{
case 'test1':
case 'test2':
return 850;
break;
default:
return 950;
}
Upvotes: 1
Reputation: 1175
I think this is as close as you can get.
switch(get_option('my_template'))
{
case 'test1':
case 'test2':
return 850;
break;
default:
return 950;
}
Edited with correction.
Upvotes: 1
Reputation: 3756
switch(get_option('my_template')) {
case 'test1':
case 'test2':
return 850;
break;
default:
return 950;
}
Upvotes: 34