How can I check if a given number is within a range of numbers?
If x is in range, then it must be greater than or equal to low, i.e., (x-low) >= 0. And must be smaller than or equal to high i.e., (high – x) <= 0. So if result of the multiplication is less than or equal to 0, then x is in range.
The is_int() function checks whether a variable is of type integer or not. This function returns true (1) if the variable is of type integer, otherwise it returns false.
The Number. isInteger() method returns true if a value is an integer of the datatype Number. Otherwise it returns false .
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
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With