Reputation: 890
I want my query parameter to be true unless explicitly set otherwise.
$a = filter_input(INPUT_GET, 'v', FILTER_VALIDATE_BOOLEAN,
array('options' => array('default' => true)));
var_dump($a);
However, accessing this script like /test.php?v=false prints bool(true)
.
Moreover, it always print bool(true)
on any input.
Upvotes: 1
Views: 817
Reputation: 5403
You need to pass FILTER_NULL_ON_FAILURE
as an options flag, so that your code instead of being:
$a = filter_input(INPUT_GET, 'v', FILTER_VALIDATE_BOOLEAN, array('options' => array('default' => true)));
It should be:
$a = filter_input(INPUT_GET, 'v', FILTER_VALIDATE_BOOLEAN, array('options' => array('default' => true), 'flags' => FILTER_NULL_ON_FAILURE));
Upvotes: 3