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;
}
A switch statement includes multiple cases that include code blocks to execute. A break keyword is used to stop the execution of case block. A switch case can be combined to execute same code block for multiple cases.
use multiple constants per case, separated by commas, and also there are no more value breaks: To yield a value from a switch expression, the break with value statement is dropped in favor of a yield statement.
You can use multiple conditions in the switch case same as using in JavaScript if statement. You can do that but the switch statement will switch on the result of the expression you provide. Given you have a logical and ( && ) in your expression there are two possible outcomes defined on how && works.
You can use multiple break statements inside a single case block in JavaScript and it will act just like multiple return statements inside of a single function.
switch(get_option('my_template')) {
case 'test1':
case 'test2':
return 850;
break;
default:
return 950;
}
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With