如何检查PHP中的整数是否在数字范围内?

时间:2011-01-13 18:55:35

标签: php range int

如何检查给定数字是否在数字范围内?

15 个答案:

答案 0 :(得分:96)

您可以使用filter_var

filter_var(
    $yourInteger, 
    FILTER_VALIDATE_INT, 
    array(
        'options' => array(
            'min_range' => $min, 
            'max_range' => $max
        )
    )
);

这也允许您specify whether you want to allow octal and hex notation of integers。请注意,该功能是类型安全的。 5.5不是整数,而是浮点数,不会验证。

有关使用PHP过滤数据的详细教程:

答案 1 :(得分:93)

表达式:

 ($min <= $value) && ($value <= $max)
如果$value介于$min$max之间,则

将为真,包含

有关比较运算符的更多信息,请参阅PHP docs

答案 2 :(得分:21)

可能有帮助:

=LEFT(A1,SEARCH(" ",A1,17)-1) & "_" & MID(A1,SEARCH(" ",A1,17),LEN(A1)-SEARCH(" ",A1,17)+1)

http://php.net/manual/en/function.range.php

答案 3 :(得分:11)

你可以用一个小辅助功能来做到这一点:

/**
 * 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...

答案 4 :(得分:7)

if (($num >= $lower_boundary) && ($num <= $upper_boundary)) {

如果希望边界值无效,可能需要调整比较运算符。

答案 5 :(得分:3)

您可以尝试以下一个声明:

if (($x-$min)*($x-$max) < 0)

或:

if (max(min($x, $max), $min) == $x)

答案 6 :(得分:1)

其他一些可能性:

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)

答案 7 :(得分:0)

使用开关盒

    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;
     }

答案 8 :(得分:0)

使用简单的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;
}

答案 9 :(得分:0)

我创建了一个函数来检查数组中的时间是否以某种方式重叠:

    /**
     * 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;
    }

答案 10 :(得分:0)

这是我的小贡献:

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];
  }

答案 11 :(得分:0)

非常感谢,我通过在 foreach 循环中添加 break 得到了我的答案,现在它工作正常。

这里是更新的答案:

foreach ($this->crud->getDataAll('shipping_charges') as $ship) {
  if ($weight >= $ship->low && $weight <= $ship->high) {
      $val = $ship->amount;
      break;
      }
      else
      {
        $val = 900;
      }
     }
     echo $val ;

答案 12 :(得分:0)

我有我的情况的功能

使用:

echo checkRangeNumber(0);
echo checkRangeNumber(1);
echo checkRangeNumber(499);
echo checkRangeNumber(500);
echo checkRangeNumber(501);
echo checkRangeNumber(3001);
echo checkRangeNumber(999);

//返回

0
1-500
1-500
1-500
501-1000
3000-3500
501-1000

function checkRangeNumber($number, $per_page = 500)
{
    //$per_page = 500; // it's fixed number, but... 

    if ($number == 0) {
        return "0";
    }

    $num_page = ceil($number / $per_page); // returns 65
    $low_limit = ($num_page - 1) * $per_page + 1; // returns 32000
    $up_limit = $num_page * $per_page; // returns 40
    return  "$low_limit-$up_limit";
}

答案 13 :(得分:-1)

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

答案 14 :(得分:-1)

$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;
        }
    }