Oto Shavadze
Oto Shavadze

Reputation: 42853

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

Upvotes: 6

Views: 1975

Answers (2)

Tobias
Tobias

Reputation: 1682

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

Upvotes: 12

Esailija
Esailija

Reputation: 140236

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

Upvotes: 3

Related Questions