如何检查一个整数是否在 PHP 中的数字范围内?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4684023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if an integer is within a range of numbers in PHP?
提问by Richard Peers
How can I check if a given number is within a range of numbers?
如何检查给定数字是否在数字范围内?
回答by Dancrumb
回答by Gordon
You can use filter_var
您可以使用 filter_var
filter_var(
$yourInteger,
FILTER_VALIDATE_INT,
array(
'options' => array(
'min_range' => $min,
'max_range' => $max
)
)
);
This will also allow you to specify whether you want to allow octal and hex notation of integers. Note that the function is type-safe. 5.5
is not an integer but a float and will not validate.
这也将允许您指定是否要允许整数的八进制和十六进制表示法。请注意,该函数是类型安全的。5.5
不是整数而是浮点数并且不会验证。
Detailed tutorial about filtering data with PHP:
PHP过滤数据的详细教程:
回答by Bobz
Might help:
可能有帮助:
if ( in_array(2, range(1,7)) ) {
echo 'Number 2 is in range 1-7';
}
回答by Luke
You could whip up a little helper function to do this:
你可以创建一个小辅助函数来做到这一点:
/**
* Determines if $number is between $min and $max
*
* @param integer $number The number to test
* @param integer $min The minimum value in the range
* @param integer $max The maximum value in the range
* @param boolean $inclusive Whether the range should be inclusive or not
* @return boolean Whether the number was in the range
*/
function in_range($number, $min, $max, $inclusive = FALSE)
{
if (is_int($number) && is_int($min) && is_int($max))
{
return $inclusive
? ($number >= $min && $number <= $max)
: ($number > $min && $number < $max) ;
}
return FALSE;
}
And you would use it like so:
你会像这样使用它:
var_dump(in_range(5, 0, 10)); // TRUE
var_dump(in_range(1, 0, 1)); // FALSE
var_dump(in_range(1, 0, 1, TRUE)); // TRUE
var_dump(in_range(11, 0, 10, TRUE)); // FALSE
// etc...
回答by lonesomeday
if (($num >= $lower_boundary) && ($num <= $upper_boundary)) {
You may want to adjust the comparison operators if you want the boundary values not to be valid.
如果您希望边界值无效,您可能需要调整比较运算符。
回答by kenorb
You can try the following one-statement:
您可以尝试以下单一语句:
if (($x-$min)*($x-$max) < 0)
or:
或者:
if (max(min($x, $max), $min) == $x)
回答by Link
I created a function to check if times in an array overlap somehow:
我创建了一个函数来检查数组中的时间是否以某种方式重叠:
/**
* Function to check if there are overlapping times in an array of \DateTime objects.
*
* @param $ranges
*
* @return \DateTime[]|bool
*/
public function timesOverlap($ranges) {
foreach ($ranges as $k1 => $t1) {
foreach ($ranges as $k2 => $t2) {
if ($k1 != $k2) {
/* @var \DateTime[] $t1 */
/* @var \DateTime[] $t2 */
$a = $t1[0]->getTimestamp();
$b = $t1[1]->getTimestamp();
$c = $t2[0]->getTimestamp();
$d = $t2[1]->getTimestamp();
if (($c >= $a && $c <= $b) || $d >= $a && $d <= $b) {
return true;
}
}
}
}
return false;
}
回答by Miquel
Here is my little contribution:
这是我的小贡献:
function inRange($number) {
$ranges = [0, 13, 17, 24, 34, 44, 54, 65, 200];
$n = count($ranges);
while($n--){
if( $number > $ranges[$n] )
return $ranges[$n]+1 .'-'. $ranges[$n + 1];
}
回答by Gajen Sunthara
Another way to do this with simple if/else range. For ex:
使用简单的 if/else 范围执行此操作的另一种方法。例如:
$watermarkSize = 0;
if (($originalImageWidth >= 0) && ($originalImageWidth <= 640)) {
$watermarkSize = 10;
} else if (($originalImageWidth >= 641) && ($originalImageWidth <= 1024)) {
$watermarkSize = 25;
} else if (($originalImageWidth >= 1025) && ($originalImageWidth <= 2048)) {
$watermarkSize = 50;
} else if (($originalImageWidth >= 2049) && ($originalImageWidth <= 4096)) {
$watermarkSize = 100;
} else {
$watermarkSize = 200;
}
回答by Nabil Kadimi
Some other possibilities:
其他一些可能性:
if (in_array($value, range($min, $max), true)) {
echo "You can be sure that $min <= $value <= $max";
}
Or:
或者:
if ($value === min(max($value, $min), $max)) {
echo "You can be sure that $min <= $value <= $max";
}
Actually this is what is use to cast a value which is out of the range to the closest end of it.
实际上,这用于将超出范围的值投射到它的最近端。
$value = min(max($value, $min), $max);
Example
例子
/**
* This is un-sanitized user input.
*/
$posts_per_page = 999;
/**
* Sanitize $posts_per_page.
*/
$posts_per_page = min(max($posts_per_page, 5), 30);
/**
* Use.
*/
var_dump($posts_per_page); // Output: int(30)