多条件 switch 语句?
这是我在 switch()
的 PHPdoc 中没有看到的东西,所以我不确定它是否可能,但我想要一个多条件的情况,例如:
switch($this) {
case "yes" || "maybe":
include "filename.php";
break;
...
}
这是有效的语法/是否可以通过 switch()
语句实现?
This is something that I haven't seen in the PHPdoc for switch()
so I'm not sure if it's possible, but I'd like to have a case which is multi-conditional, such as:
switch($this) {
case "yes" || "maybe":
include "filename.php";
break;
...
}
Is this valid syntax/is this even possible with a switch()
statement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
通常你只会用例失败。
Usually you'd just use case fall-through.
不,不。该表达式将被计算为
("yes" or "maybe")
,结果为true
。然后switch
将针对该结果进行测试。你想使用
No and no. The expression will be evaluated as
("yes" or "maybe")
, which will result intrue
.switch
will then test against that result.You want to use
应该是
Should be
您可以通过失败来做到这一点:
You can do this with fall-through:
当然,只需指定两种情况而不破坏第一个情况,如下所示:
如果不破坏一种情况,则该情况的任何代码都会运行,并继续执行其他代码,直到执行被破坏。它将继续遍历其下面的所有情况,直到看到
中断
。Sure, just specify two cases without breaking the first one, like so:
If you don't break a case, then any code for that case is run and continues on to execute additional code until the execution is broken. It will continue through all cases below it until it sees a
break
.