PHP validation booleans using filter_var
Asked Answered
C

4

10

I'm using filter_var to validate boolean values but I did not expect it to not recognize FALSE. Why does this happen?

filter_var(FALSE, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)

returns

null

Chemosynthesis answered 3/2, 2012 at 16:51 Comment(9)
you want to validate the bool FALSE?Opinionative
read this: devshed.com/c/a/PHP/…Opinionative
well yes, I'm wondering why TRUE returns TRUE but FALSE returns nullChemosynthesis
Looks buggy. filter_var(false, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) is null, filter_var(0, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) is false. I'm on 5.3.3Sams
You could avoid ambiguity by using a (bool) cast on the result: $val = FALSE; $val = (bool) filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);Bourges
@powtac Are you sure, it returns true? On my machine filter_var(false, FILTER_VALIDATE_BOOLEAN) (w/o FILTER_NULL_ON_FAILURE) is false.Sams
I get the same result (false)Chemosynthesis
@Sams Sorry guys, I got false as well. Deleted my wrong comment.Marmot
Found this: bugs.php.net/bug.php?id=49510 -- unfortunately, by the date of the report, there doesn't seem to be any rush to fixing thisChemosynthesis
I
7

filter_var is new as of PHP 5.2. You've run into a known bug: https://bugs.php.net/bug.php?id=49510 Feel free to vote on or comment on that bug.

You're trying to do something like this:

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

There are a number of cheap workarounds like this:

$v = $v===FALSE ? FALSE : filter_var($v, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)
Ignacia answered 3/2, 2012 at 19:57 Comment(0)
A
3

It sounds like this is actually how it's supposed to work, strangely enough (yes, my mind was blown by that). From https://bugs.php.net/bug.php?id=51344

This is going to sound insane when you've looked at the underlying filter code, but this is actually correct according to the documentation: the default behaviour of filter_input() is to return NULL for non-existent inputs and false when validation fails, and FILTER_NULL_ON_FAILURE simply flips that behaviour to false for non-existent inputs and NULL on validation failure. (No, I don't have a clue where that would be useful either, and the name of the flag is unfortunate in the filter_input() context, since it implies that NULL wouldn't normally be returned. It makes more sense when used with filter_var(), which doesn't have the non-existent input case.)

[table omitted due to SO formatting]

I'll pop a comment into the filter_input() and filter_input_array() implementations to note that this is by design, even though the code does kind of look wrong.

Closing Won't Fix.

Aluin answered 30/1, 2014 at 23:12 Comment(0)
L
1

This was the behaviour when filter_var was first introduced with version 5.2 and resolved at some point after 5.4 as is seen by this https://3v4l.org/Cv1MZ

Starting from version 5.4 this is what happens:

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

bool(false)

which makes much more sense.

Latter answered 26/1, 2018 at 15:11 Comment(0)
G
0

According to the documentation

Returns true for "1", "true", "on" and "yes". Returns false otherwise.

Anything different than the values mentioned above is considered falsy. This is not to test if a variable is actually a boolean like with typeof or is_bool(), but more like a mean to test if the input comming from the user (usually an <input type="checkbox"> form) is truthy/falsy.

The behavior of this function could be understand as let it go through if it's correct rather than a mean to test the type of a variable (we have lots of other functions for this). Exemple :

filter_var('435345', FILTER_VALIDATE_INT)

The type is a string, but the result is not true/false as the intention is not to validate the type. So an int would be returned (let go through).

Groundsheet answered 22/2, 2022 at 11:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.