I am trying check if variable is integer number and if she is in diapason: 2-15.
I am trying make this using filter_var()
function. but I dont understood how correct use min_range
and max_range
parameters.
this not works, where I am wrong?
$c = 48;
if ( filter_var($c, FILTER_VALIDATE_INT , array("min_range"=>2,"max_range"=>15)) === false ) {
echo "bad";
}
filter_var() is a PHP function used to filters a variable with the help of a specified filter. In PHP programming language we can use filter_var() function to validate and sanitize a data such as email id, IP address etc.
filter_var. If a variable doesn't exist, the filter_input() function returns null while the filter_var() function returns an empty string and issues a notice of an undefined index.
Return Value: It returns the filtered data on success, or FALSE on failure. $str = "<h1>GeeksforGeeks! </h1>" ; $newstr = filter_var( $str , FILTER_SANITIZE_STRING);
The min_range and max_range values have to be one level, deeper:
// for filters that accept options, use this format
$options = array(
'options' => array(
'default' => 3, // value to return if the filter fails
// other options here
'min_range' => 0
),
'flags' => FILTER_FLAG_ALLOW_OCTAL,
);
See: http://php.net/filter_var
So:
$c = 48;
if (filter_var($c, FILTER_VALIDATE_INT, array("options" => array("min_range"=>2,"max_range"=>15))) === false) {
echo "bad";
}
They should be in the options array:
if ( filter_var($c, FILTER_VALIDATE_INT, array(
"options"=>array(
"min_range"=>2,
"max_range"=>15
)
)) === false ) {
echo "bad";
}
Seems overly complicated way to write 2 <= $c && $c <= 15
though
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