[英]Using comparison operators in a PHP 'switch' statement
I have four conditions that I need to go through and I thought it would be best to use the switch
statement in PHP.我有四个条件需要通过,我认为最好在 PHP 中使用
switch
语句。 However, I need to check whether an integer is, let's say, less than or equal, or greater than and equal.但是,我需要检查一个整数是否小于等于,或者大于等于。
switch ($count) {
case 20:
$priority = 'low';
break;
case 40:
$priority = 'medium';
break;
case 60:
$priority = 'high';
break;
case 80:
$priority = 'severe';
break;
}
With an if()
statement it would look like the following:使用
if()
语句,它将如下所示:
if ($count <= 20) {
$priority = 'low';
}
if ($count <= 40) {
$priority = 'medium';
}
Is that possible in switch-case
?这在
switch-case
中可能吗?
A more general case for solving this problem is:解决此问题的更一般情况是:
switch (true) {
case $count <= 20:
$priority = 'low';
break;
case $count <= 40:
$priority = 'medium';
break;
case $count <= 60:
$priority = 'high';
break;
default:
$priority = 'severe';
break;
}
Switches can't do that, but in this particular case you can do something like this:开关不能这样做,但在这种特殊情况下,您可以执行以下操作:
switch ((int)(($count - 1) / 20)) {
case 0:
$priority = 'low';
break;
case 1:
$priority = 'medium';
break;
case 2:
$priority = 'high';
break;
case 3:
$priority = 'severe';
break;
}
So in (int)(($count - 1) / 20)
all values from 0 to 20 will eval to 0, 21 to 40 will eval to 1 and so on, allowing you to use the switch statement for this purpose.因此,在
(int)(($count - 1) / 20)
从 0 到 20 的所有值都将评估为 0,21 到 40 将评估为 1,依此类推,允许您为此目的使用 switch 语句。
And since we are concatenating values, we can even simplify to an array:由于我们正在连接值,我们甚至可以简化为一个数组:
$priorities = ['low', 'medium', 'high', 'severe'];
$priority = $priorities[(int)(($count - 1) / 20)];
There is a way that works in PHP 7 using ternary assignment operators.有一种方法可以在 PHP 7 中使用三元赋值运算符。 The operator was introduced earlier on (5.4?), but I never tested the code on other versions.
运算符是在 (5.4?) 上较早引入的,但我从未在其他版本上测试过代码。 I wrote the whole switch code there , however for brevity here is just the specific clause.
我在那里写了整个 switch 代码,但是为了简洁起见,这里只是特定的条款。 Let's say we want the condition to match for all numbers greater than or equal to five:
假设我们希望条件匹配所有大于或等于 5 的数字:
switch($value){
case ($value >= 5 ? $value : !$value): // Do something here
break;
}
We either allow the $value to pass unchanged or we negate the value according to the condition.我们要么允许 $value 不变地传递,要么根据条件否定该值。 A $value will always match itself or fail the test against its negation.
$value 将始终与自身匹配或无法通过其否定的测试。
No. switch()
statements are for doing multiple equality tests.否。
switch()
语句用于进行多个相等性测试。 They're basically just a slightly easier to read (but also more hazardous) version of它们基本上只是一个更容易阅读(但也更危险)的版本
if (x == 'a') { ... }
else if (x == 'b') { ... }
else if (x == 'c') { ... }
code.代码。 There is no way to change a
switch()
away from ==
to <
or any other comparison operator.无法将
switch()
从==
更改为<
或任何其他比较运算符。 It's strictly for equality testing.它严格用于平等测试。
Using the ternary operator :使用三元运算符:
$priority =
// "switch" comparison for $count
$count <= 20 ? 'low' :
($count <= 40 ? 'medium' :
($count <= 60 ? 'high' :
// default above 60
'severe'));
I know the common complaint that ternary operators can be hard to understand, but this is just too easy to do with a simple ?:
.我知道常见的抱怨是三元运算符可能难以理解,但使用简单的
?:
就太容易了。
It operates like the Excel "If" formula:它的操作类似于Excel 的“If”公式:
=IF( logical_test, value_if_true, value_if_false )
$variable = logical_test ? value_if_true : value_if_false
And you can nest the if statements (place a second ?:
in the 'value_if_false' section of the first one), which is certainly where it can become confusing to read, but less so when you write it out line by line, as above.并且您可以嵌套if语句(在第一个语句的“value_if_false”部分放置第二个
?:
),这肯定是阅读起来会令人困惑的地方,但是当您一行一行地写出来时就不会那么容易了,如上所述.
My code above is basically equivalent to the if()
statement written by the OP.我上面的代码基本上等同于 OP 编写的
if()
语句。
I can also confirm bytepunk's answer here is functional.我也可以确认字节朋克在这里的回答是有效的。
Also, expending the concept with PHP 7:另外,用 PHP 7 扩展这个概念:
switch ($interval->days)
{
case 0:
return '1 day';
// break;
case (($interval->days >= 1 && $interval->days <= 7) ?? $interval->days):
return '1 week';
// break;
case (($interval->days >= 8 && $interval->days <= 31) ?? $interval->days):
return '1 month';
// break;
case (($interval->days >= 31 && $interval->days <= 93) ?? $interval->days):
return '2-3 months';
// break;
default:
return '3+ months';
}
I will admit that this isn't the cleanest of code, so perhaps wrapping each case with a static-like pure function would neat things up a bit, and not forgetting to name each function (or create one generic function with parameters) to match the case.我承认这不是最干净的代码,所以也许用一个类似静态的纯函数包装每个案例会让事情变得更整洁一点,并且不要忘记命名每个函数(或创建一个带参数的通用函数)以匹配案子。 This will make it more readable.
这将使其更具可读性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.