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

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

Richard Peers asked 2019-11-09T00:37:50Z
11个解决方案
102 votes

表达方式:

 ($min <= $value) && ($value <= $max)

如果$value$min$max之间(包括两端),则为true

有关比较运算符的更多信息,请参见PHP文档。

Dancrumb answered 2019-11-09T00:38:15Z
102 votes

您可以使用5.5

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

这还将允许您指定是否要允许整数的八进制和十六进制表示法。 请注意,该函数是类型安全的。 5.5不是整数,而是浮点数,将无法验证。

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

  • [https://phpro.org/tutorials/Filtering-Data-with-PHP.html]
Gordon answered 2019-11-09T00:38:59Z
25 votes

可能有帮助:

if ( in_array(2, range(1,7)) ) {
    echo 'Number 2 is in range 1-7';
}

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

Bobz answered 2019-11-09T00:39:30Z
13 votes

您可以启动一些辅助函数来执行此操作:

/**
 * 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...
Luke answered 2019-11-09T00:39:56Z
7 votes
if (($num >= $lower_boundary) && ($num <= $upper_boundary)) {

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

lonesomeday answered 2019-11-09T00:40:21Z
3 votes

您可以尝试以下一种陈述:

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

要么:

if (max(min($x, $max), $min) == $x)
kenorb answered 2019-11-09T00:40:46Z
0 votes

使用开关盒

    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;
     }
Jefkine Kafunah answered 2019-11-09T00:41:10Z
0 votes

使用简单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;
}
Gajen Sunthara answered 2019-11-09T00:41:35Z
0 votes

其他可能性:

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)
Nabil Kadimi answered 2019-11-09T00:42:09Z
0 votes

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

    /**
     * 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;
    }
Link answered 2019-11-09T00:42:34Z
-1 votes
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
Justin Levene answered 2019-11-09T00:42:51Z
translate from https://stackoverflow.com:/questions/4684023/how-to-check-if-an-integer-is-within-a-range-of-numbers-in-php