I want the input to accpet 'true' or 'false' only, so I am trying to use FILTER_VALIDATE_BOOLEAN
to do that,
if (!filter_var('false', FILTER_VALIDATE_BOOLEAN))
{
$error = true;
echo 'error';
}
It echoes the error message but it should not.
How can I make it right?
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:
Returns the boolean
false
.
Returns the boolean
true
.
Returns
NULL
.That's why I added the second option. The input string does neither classify as
true
norfalse
.So you actually have to check with
=== NULL
for your condition. That would give you the result you probably desired: