Have a look at the manual http://www.php.net/manual/en/filter.filters.validate.php
Your syntax/usage is correct. filter_var('false', FILTER_VALIDATE_BOOLEAN)
however does not verify the pattern, it converts the input string into the accordingly typed value.
To make it explicit:
var_dump(filter_var('false', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));
Returns the boolean false
.
var_dump(filter_var('true', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));
Returns the boolean true
.
var_dump(filter_var('wrong', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));
Returns NULL
.
That's why I added the second option. The input string does neither classify as true
nor false
.
So you actually have to check with === NULL
for your condition. That would give you the result you probably desired:
if (filter_var('false', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) === NULL)
{
echo "Input did not evaluate to boolean 'false' nor 'true'"
'false'
is not a boolean, it's a string. – Harris'false'
should be justfalse
in this case otherwise it will be treated as a string. – Antimacassarfalse
... – Lurid