doc-en
doc-en copied to clipboard
Examples for filter_var
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.
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.