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

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

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

13条回答
  •  佛祖请我去吃肉
    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

提交回复
热议问题