如何检查给定数字是否在数字范围内?
Answers:
$value
为float时,这会产生错误的结果。另外,由于比较比较松散,因此传递字符串可能会产生错误的结果,例如(0 <= 'string') && ('string' <= 10) );
,true
由于类型杂乱所致。
(int)
演员表,他们会开始感到嘈杂。在那种情况下,我可能会选择将它们排除在外。
您可以使用 filter_var
filter_var(
$yourInteger,
FILTER_VALIDATE_INT,
array(
'options' => array(
'min_range' => $min,
'max_range' => $max
)
)
);
这也将允许您指定是否要允许整数的八进制和十六进制表示法。请注意,该函数是类型安全的。5.5
不是整数,而是浮点数,将无法验证。
有关使用PHP过滤数据的详细教程:
可能有帮助:
if ( in_array(2, range(1,7)) ) {
echo 'Number 2 is in range 1-7';
}
您可以启动一些辅助函数来执行此操作:
/**
* 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;
}
您将像这样使用它:
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...
使用开关盒
switch ($num){
case ($num>= $value1 && $num<= $value2):
echo "within range 1";
break;
case ($num>= $value3 && $num<= $value4):
echo "within range 2";
break;
.
.
.
.
.
default: //default
echo "within no range";
break;
}
switch(true)
,否则为$num == 0
,大小写逻辑将失败,因为PHP试图匹配0 == ($num>= $value1 && $num<= $value2)
,等等。我建议将此作为编辑。
if (in_array($value, range($min, $max), true)) {
echo "You can be sure that $min <= $value <= $max";
}
if ($value === min(max($value, $min), $max)) {
echo "You can be sure that $min <= $value <= $max";
}
实际上,这是用于转换超出其最接近范围范围的值的方法。
$value = min(max($value, $min), $max);
/**
* 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)
使用简单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;
}
我创建了一个函数来检查数组中的时间是否以某种方式重叠:
/**
* 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;
}
function limit_range($num, $min, $max)
{
// Now limit it
return $num>$max?$max:$num<$min?$min:$num;
}
$min = 0; // Minimum number can be
$max = 4; // Maximum number can be
$num = 10; // Your number
// Number returned is limited to be minimum 0 and maximum 4
echo limit_range($num, $min, $max); // return 4
$num = 2;
echo limit_range($num, $min, $max); // return 2
$num = -1;
echo limit_range($num, $min, $max); // return 0
$ranges = [
1 => [
'min_range' => 0.01,
'max_range' => 199.99
],
2 => [
'min_range' => 200.00,
],
];
foreach( $ranges as $value => $range ){
if( filter_var( $cartTotal, FILTER_VALIDATE_FLOAT, [ 'options' => $range ] ) ){
return $value;
}
}