如何使用开关盒“或” 在 PHP 中
有没有办法在 PHP 开关中使用“OR”运算符或等效运算符?
例如,这样的事情:
switch ($value) {
case 1 || 2:
echo 'the value is either 1 or 2';
break;
}
Is there a way of using an 'OR' operator or equivalent in a PHP switch?
For example, something like this:
switch ($value) {
case 1 || 2:
echo 'the value is either 1 or 2';
break;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
这称为“穿过”箱子块。 该术语存在于大多数实现 switch 语句的语言中。
This is called "falling through" the case block. The term exists in most languages implementing a switch statement.
如果您必须将
||
与switch
一起使用,那么您可以尝试:如果不是,您的首选解决方案是
问题是,这两种方法在处理大型案例时都效率不高。 ..想象一下
1
到100
这会完美地工作If you must use
||
withswitch
then you can try :If not your preferred solution would have been
The issue is that both method is not efficient when dealing with large cases ... imagine
1
to100
this would work perfectly我不会重新发布其他答案,因为它们都是正确的,但我只是补充一点,您不能使用 switch 来执行更“复杂”的语句,例如:测试某个值是否“大于 3”、“ 4 到 6" 之间,等等。如果您需要执行类似的操作,请坚持使用
if
语句,或者如果特别强烈需要switch
那么可以从后到前使用它:但正如我所说,我个人会在那里使用
if
语句。I won't repost the other answers because they're all correct, but I'll just add that you can't use switch for more "complicated" statements, eg: to test if a value is "greater than 3", "between 4 and 6", etc. If you need to do something like that, stick to using
if
statements, or if there's a particularly strong need forswitch
then it's possible to use it back to front:but as I said, I'd personally use an
if
statement there.尝试使用本文中的以下示例:[http://phpswitch.com/][1]
可能的 Switch 情况:
(i)。 一个简单的 switch 语句
switch 语句是奇妙而神奇的。 它是语言的一部分,允许您在值的不同选项之间进行选择,并根据设置的值运行不同的代码段。
每个可能的选项都由 switch 语句中的一个 case 给出。
示例:
(ii)。 分隔代码块
switch 的主要警告是,每种情况都会运行到下一种情况,除非您用break 来停止它。 如果将上述简单情况扩展到情况 5:
示例:
(iii)。 在多种情况下使用fallthrough
因为switch将继续运行代码直到找到中断,所以很容易采用fallthrough的概念并在多个情况下运行相同的代码:
示例:
案例 2:
(iv)。 高级切换:条件情况
PHP 的开关不仅仅允许您切换特定变量的值:您可以使用任何表达式作为其中一种情况,只要因为它给出了要使用的案例的值。 作为示例,这里是一个使用 switch 编写的简单验证器:
示例:
我认为这可以帮助您解决问题。
Php Switch 具有不同语句的多个条件,例如 if else if 但它只能检查条件浮点的相等性,但不能通过 PHP 开关。
例如 :
Try with these following examples in this article : [http://phpswitch.com/][1]
Possible Switch Cases :
(i). A simple switch statement
The switch statement is wondrous and magic. It's a piece of the language that allows you to select between different options for a value, and run different pieces of code depending on which value is set.
Each possible option is given by a case in the switch statement.
Example :
(ii). Delimiting code blocks
The major caveat of switch is that each case will run on into the next one, unless you stop it with break. If the simple case above is extended to cover case 5:
Example :
(iii). Using fallthrough for multiple cases
Because switch will keep running code until it finds a break, it's easy enough to take the concept of fallthrough and run the same code for more than one case:
Example :
case 2:
(iv). Advanced switching: Condition cases
PHP's switch doesn't just allow you to switch on the value of a particular variable: you can use any expression as one of the cases, as long as it gives a value for the case to use. As an example, here's a simple validator written using switch:
Example :
I think this may help you to resolve your problem.
Php Switch Multiple conditions having different statements like-if else if but it can check only equality of the condition floating point but value can’t be check by the PHP Switch.
For Example :
尝试
Try
我建议您通过 switch (手动的)。
解释
就像您想要执行单个语句的值一样,您可以将其不带break,就像直到或除非找到break一样。 它将继续执行代码,如果发现中断,它将脱离switch情况。
I suggest you to go through switch (manual).
Explanation
Like for the value you want to execute a single statement for, you can put it without a break as as until or unless break is found. It will go on executing the code and if a break found, it will come out of the switch case.
匹配表达式 (PHP 8)
PHP 8 引入了一个新的 < code>match 表达式与
switch
类似,但语法更短,差异如下:break
语句UnhandledMatchError
)示例:
Match expression (PHP 8)
PHP 8 introduced a new
match
expression that is similar toswitch
but with the shorter syntax and these differences:break
statementsUnhandledMatchError
will be thrown)Example:
请注意,您还可以使用闭包将 switch 的结果(使用早期返回)分配给变量:
当然,这种方法已经过时了,因为它正是新 PHP 8 匹配函数,如 _dom93 答案。
Note that you can also use a closure to assign the result of a switch (using early returns) to a variable:
Of course this way to do is obsolete as it is exactly the purpose of the new PHP 8 match function as indicated in _dom93 answer.
使用此代码:
为 1 和 2 调用该块。
Use this code:
The block is called for both 1 and 2.
http://php.net/manual/en/control-structs.switch。 php
例子
http://php.net/manual/en/control-structures.switch.php
Example
最好的方法可能是 if else 和请求。 此外,这可以更容易、更清晰地使用。
示例:
不要使用不能很好地与 PHP 配合使用的函数,尤其是当您在 HTML 中使用 PHP 时。
The best way might be if else with requesting. Also, this can be easier and clear to use.
Example:
Instead of using the functions that won't work well with PHP, especially when you have PHP in HTML.