Search code examples
phpfilter-var

php filter var returning a wrong result


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.


Solution

  • 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.