doc-en icon indicating copy to clipboard operation
doc-en copied to clipboard

Examples for filter_var

Open gesior opened this issue 3 years ago • 1 comments

From manual page: https://php.net/function.filter-var


There is no example with if and I saw a lot of implementations like:

if (!filter_var($value, $type)) {
    echo 'error';
}

and it should be:

if (filter_var($value, $type) !== $value) {
    echo 'error';
}

Especially, when you validated with FILTER_VALIDATE_BOOLEAN filter.

gesior avatar Mar 23 '22 09:03 gesior

Comparing to the passed value won't necessarily work for sanitizing filters. You usually want to compare against === false, unless you're using FILTER_VALIDATE_BOOL, in which case it might be best to specify a default. And of course, if you set FILTER_NULL_ON_FAILURE, comparison against === null is needed.

cmb69 avatar Mar 23 '22 10:03 cmb69