如何检查一个整数是否在一个范围内?
有没有办法在不执行以下冗余代码的情况下测试范围:
if ($int>$min && $int<$max)
?
就像一个函数:
function testRange($int,$min,$max){
return ($min<$int && $int<$max);
}
用法:
if (testRange($int,$min,$max))
?
PHP有这样的内置函数吗?或者有其他方法可以做到吗?
Is there a way to test a range without doing this redundant code:
if ($int>$min && $int<$max)
?
Like a function:
function testRange($int,$min,$max){
return ($min<$int && $int<$max);
}
usage:
if (testRange($int,$min,$max))
?
Does PHP have such built-in function? Or any other way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
用 1000000 次循环测试了你的 3 种方法。
t1_test1:
($val >= $min && $val <= $max)
: 0.3823 mst2_test2:
(in_array($val, range($min, $max ) ))
: 9.3301 mst3_test3:
(max(min($var, $max), $min) == $val)
: 0.7272 msT1 是最快的,基本上是这样的:
Tested your 3 ways with a 1000000-times-loop.
t1_test1:
($val >= $min && $val <= $max)
: 0.3823 mst2_test2:
(in_array($val, range($min, $max))
: 9.3301 mst3_test3:
(max(min($var, $max), $min) == $val)
: 0.7272 msT1 was fastest, it was basicly this:
我认为你不会得到比你的功能更好的方法。
它很干净,易于遵循和理解,并返回条件的结果(没有
return (...) ? true : false
混乱)。I don't think you'll get a better way than your function.
It is clean, easy to follow and understand, and returns the result of the condition (no
return (...) ? true : false
mess).没有内置函数,但您可以通过适当调用函数
min()
和max()
轻松实现。There is no builtin function, but you can easily achieve it by calling the functions
min()
andmax()
appropriately.大多数给定的示例假设对于测试范围 [$a..$b],$a <= $b,即范围极值处于较低 - 高阶,并且大多数假设都是整数。
但我需要一个函数来测试 $n 是否在 $a 和 $b 之间,如下所述:
有一种简单的测试方法。
我进行测试的基础是,当 $n 在 $a 和 $ 之间时,
($n-$a)
和($n-$b)
有不同的符号b,当 $n 超出 $a..$b 范围时,符号相同。该函数对测试递增、递减、正负数均有效,不限于仅测试整数。
Most of the given examples assume that for the test range [$a..$b], $a <= $b, i.e. the range extremes are in lower - higher order and most assume that all are integer numbers.
But I needed a function to test if $n was between $a and $b, as described here:
There is an easy way to test.
I base the test it in the fact that
($n-$a)
and($n-$b)
have different signs when $n is between $a and $b, and the same sign when $n is outside the $a..$b range.This function is valid for testing increasing, decreasing, positive and negative numbers, not limited to test only integer numbers.
我无法发表评论(没有足够的声誉),所以我将在这里修改 Luis Rosety 的答案:
此函数也适用于 n == a 或 n == b 的情况。
证明:
令 n 属于范围 [a,b],其中 [a,b] 是实数的子集。
现在 a <= n <= b。那么 na >= 0 且 nb <= 0。这意味着 (na)*(nb) <= 0。
情况 b <= n <= a 的工作原理类似。
I'm not able to comment (not enough reputation) so I'll amend Luis Rosety's answer here:
This function works also in cases where n == a or n == b.
Proof:
Let n belong to range [a,b], where [a,b] is a subset of real numbers.
Now a <= n <= b. Then n-a >= 0 and n-b <= 0. That means that (n-a)*(n-b) <= 0.
Case b <= n <= a works similarly.
还有
filter_var()
,它是检查范围的本机函数。它并没有给出你想要的(永远不会返回true),但是通过“cheat”我们可以改变它。我认为就可读性而言这不是一个好的代码,但我表明它是一种可能性:
只需填写
$someNumber
、$min
和$max.使用该过滤器的
filter_var
当数字超出范围时返回布尔值 false,或者当数字在范围内时返回数字本身。当数字在范围内时,表达式 (!== false
) 使函数返回 true。如果你想以某种方式缩短它,请记住类型转换。如果您使用
!=
,则对于 -5 范围内的数字 0 将为 false; +5(虽然这应该是真的)。如果您使用类型转换 ((bool)
),也会发生同样的情况。想象一下(来自其他答案):
如果用户写入空值(
$userScore = ''
),那么它将是正确的,因为in_array
在这里设置为默认值,非-更严格,这意味着范围也会创建0
,并且'' == 0
(非严格),但是'' !== 0
code> (如果您使用严格模式)。人们很容易错过这些事情,这就是为什么我写了一些相关内容。 我了解到严格运算符是默认的,程序员只能在特殊情况下使用非严格。我认为这是一个很好的教训。这里的大多数示例在某些情况下都会失败,因为检查不严格。我仍然喜欢filter_var,您可以使用上面(或下面,如果我得到了如此“升级”;))函数并创建您自己的回调,您可以将其用作
FILTER_CALLBACK
过滤器。您可以返回 bool 甚至添加 openRange 参数。还有其他好处:您可以使用其他功能,例如检查每个数字的范围数组或 POST/GET 值。这确实是一个非常强大的工具。There's
filter_var()
as well and it's the native function which checks range. It doesn't give exactly what you want (never returns true), but with "cheat" we can change it.I don't think it's a good code as for readability, but I show it's as a possibility:
Just fill
$someNumber
,$min
and$max
.filter_var
with that filter returns either boolean false when number is outside range or the number itself when it's within range. The expression (!== false
) makes function return true, when number is within range.If you want to shorten it somehow, remember about type casting. If you would use
!=
it would be false for number 0 within range -5; +5 (while it should be true). The same would happen if you would use type casting ((bool)
).Imagine that (from other answer):
If user would write empty value (
$userScore = ''
) it would be correct, asin_array
is set here for default, non-strict more and that means that range creates0
as well, and'' == 0
(non-strict), but'' !== 0
(if you would use strict mode). It's easy to miss such things and that's why I wrote a bit about that. I was learned that strict operators are default, and programmer could use non-strict only in special cases. I think it's a good lesson. Most examples here would fail in some cases because non-strict checking.Still I like filter_var and you can use above (or below if I'd got so "upped" ;)) functions and make your own callback which you would use as
FILTER_CALLBACK
filter. You could return bool or even addopenRange
parameter. And other good point: you can use other functions, e.g. checking range of every number of array or POST/GET values. That's really powerful tool.您可以使用
in_array()
结合range()
注意 正如评论中所指出的,这并不完全是一个如果您注重性能,这是一个很好的解决方案。生成数组(尤其是范围较大的数组)会减慢执行速度。
You could do it using
in_array()
combined withrange()
Note As has been pointed out in the comments however, this is not exactly a great solution if you are focussed on performance. Generating an array (escpecially with larger ranges) will slow down the execution.
使用比较运算符比调用任何函数要快得多。我不能百分百确定这是否存在,但我认为它不存在。
Using comparison operators is way, way faster than calling any function. I'm not 100% sure if this exists, but I think it doesn't.
一种可能性(不是解决方案):
One possibility (not a solution) :
这是对 @Mark Paspirgilis 提供的答案的扩展,允许可选择的包含并返回布尔响应。
This is an expansion on the answer provided by @Mark Paspirgilis, allows for selectable inclusion and returns a Boolean response.