Switch 语句 >可以在一个案例中包含多个案例匹配吗?
我想在一个开关案例中传递多个值。我意识到他们不可能以我尝试的方式做到这一点。除了将每个案例都放到网上之外,还有其他方法吗?
switch(get_option('my_template'))
{
case 'test1', 'test2':
return 850;
break;
default:
return 950;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在开关结构中,我不相信有任何方法可以在一行上执行“或”之类的操作。这将是最简单的方法:
但是,特别是如果您只返回一个值而不执行代码,我建议执行以下操作:
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:
But, especially if you are only returning a value and not executing code, i would reccomend doing the following:
除非您在您的情况下使用
break;
,否则执行只会落入下一个情况。您可以通过将每个案例堆叠在一起来利用这一点,例如:因为“test1”案例上没有
break;
,所以当执行在该案例上结束时(即立即结束,因为有其中没有逻辑),然后控制权将落入“test2”情况,该情况将以break
语句结束。在这种情况下,甚至不需要
break
,因为return
语句将负责打破switch
on它自己的。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.: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 itsbreak
statement.In this case, the
break
isn't even needed for these cases, since thereturn
statement will take care of breaking out of theswitch
on its own.我认为这已经是你所能得到的最接近的了。
编辑并修正。
I think this is as close as you can get.
Edited with correction.
这个怎么样
How about this