Checking PHP buffers using filter_var

I use filter_var to check for boolean values, but I did not expect it to recognize FALSE . Why is this happening?

filter_var(FALSE, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)

returns

null

+6
source share
3 answers

filter_var is new compared to PHP 5.2. You encountered a known error: https://bugs.php.net/bug.php?id=49510 Feel free to vote or comment on this error.

You are trying to do something like this:

 $v = filter_var($v, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) 

There are a number of cheap workarounds:

 $v = $v===FALSE ? FALSE : filter_var($v, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) 
+5
source

It seems like this is actually how it should work, oddly enough (yes, my mind was blown up by this). From https://bugs.php.net/bug.php?id=51344

This will sound crazy when you look at the filter, but it is really correct according to the documentation: the default filter_input () behavior is returning NULL for nonexistent inputs and false when the check is not performed, and FILTER_NULL_ON_FAILURE just flips this behavior to value false for nonexistent inputs and NULL when validation fails. (No, I don’t have a hint where this would be useful, and the flag name is unsuccessful in the context of filter_input (), since it implies that NULLs are usually not returned. This makes sense when used with filter_var (), which does not have non-existent input. )

[table omitted due to SO formatting]

I will post a comment in filter_input () and filter_input_array () to note that this is by design, although the code does not look right.

Closing will not be fixed.

+2
source

This was the behavior when filter_var first introduced with version 5.2 and enabled at some point after 5.4, as can be seen from this https://3v4l.org/Cv1MZ

Starting with version 5.4, this happens:

 var_dump(filter_var(FALSE, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)); 

BOOL (false)

which makes a lot more sense.

0
source

Source: https://habr.com/ru/post/907596/


All Articles