简体   繁体   中英

Using filter_var with parameters

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";
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM