Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php filter var returning a wrong result

Tags:

php

filter-var

I wanted to use the php filer_var function but it's returning a wrong result, it seems that it doesn't take into account the range:

$v = 54;

$int_opts = array (

    'min_range' => 0,
    'max_range' => 24

);

if ( filter_var($v, FILTER_VALIDATE_INT, $int_opts) ) echo 'an integer';

else echo 'not an integer';

This shouldn't be an integer as 54 is not between 0 and 24, but it returns true and echoes "an integer".

What's the problem here?

Thanks.

like image 972
medk Avatar asked Oct 17 '25 05:10

medk


1 Answers

The "options" array needs to have a member named "options". From the manual:

$options = array(
    'options' => array(
        'default' => 3, // value to return if the filter fails
        // other options here
        'min_range' => 0
    ),
    'flags' => FILTER_FLAG_ALLOW_OCTAL,
);

you're not passing that so the behaviour displayed is okay.

like image 99
Pekka Avatar answered Oct 18 '25 18:10

Pekka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!