How to check if an integer is within a range of numbers in PHP?

后端 未结 13 597
傲寒
傲寒 2020-12-04 10:57

How can I check if a given number is within a range of numbers?

相关标签:
13条回答
  • 2020-12-04 11:50

    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];
      }
    
    0 讨论(0)
  • 2020-12-04 11:50
    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
    
    0 讨论(0)
  • 2020-12-04 11:51

    Another way to do this with simple if/else range. For ex:

    $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;
    }
    
    0 讨论(0)
  • 2020-12-04 11:52

    The expression:

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

    will be true if $value is between $min and $max, inclusively

    See the PHP docs for more on comparison operators

    0 讨论(0)
  • 2020-12-04 11:52

    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)
    
    0 讨论(0)
  • 2020-12-04 11:56

    You can use 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.

    Detailed tutorial about filtering data with PHP:

    • https://phpro.org/tutorials/Filtering-Data-with-PHP.html
    0 讨论(0)
提交回复
热议问题