note 96890 added to filter.filters.validate
| From: | pravila at alumni dot calpoly dot edu | Date: | Sun, 21 Mar 2010 16:53:35 +0000 |
| Subject: | note 96890 added to filter.filters.validate | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-167273@lists.php.net to get a copy of this message | ||
Take caution when using the FILTER_VALIDATE_BOOLEAN filter as it seems to have different behaviors
when used in the filter_var() vs. the filter_input() functions.
To demonstrate, let's parse some arguments from a GET request (notice how arg2 is NOT set):
example.com/script.php?arg1=yes&arg3=no
<?php
// filtering by variable
$var1 = filter_var($_GET["arg1"], FILTER_VALIDATE_BOOLEAN);
$var2 = filter_var($_GET["arg2"], FILTER_VALIDATE_BOOLEAN);
$var3 = filter_var($_GET["arg3"], FILTER_VALIDATE_BOOLEAN);
// filtering by input
$input1 = filter_input(INPUT_GET, "arg1", FILTER_VALIDATE_BOOLEAN);
$input2 = filter_input(INPUT_GET, "arg2", FILTER_VALIDATE_BOOLEAN);
$input3 = filter_input(INPUT_GET, "arg3", FILTER_VALIDATE_BOOLEAN);
// as expected...
var_dump($var1); // bool(true)
var_dump($var2); // bool(false)
var_dump($var3); // bool(false)
// NULL is not an expected return...
var_dump($input1); // bool(true)
var_dump($input2); // NULL
var_dump($input3); // bool(false)
?>
As per the documentation, the return is limited to true XOR false unless the FILTER_NULL_ON_FAILURE
flag is set, but it seems as if this flag is set automatically with the filter_input() function.
(Note: same behavior for filter_var_array() vs. filter_input_array())
----
Server IP: 69.147.83.197
Probable Submitter: 80.174.182.168
----
Manual Page -- http://www.php.net/manual/en/filter.filters.validate.php
Edit -- https://master.php.net/note/edit/96890
Del: integrated -- https://master.php.net/note/delete/96890/integrated
Del: useless -- https://master.php.net/note/delete/96890/useless
Del: bad code -- https://master.php.net/note/delete/96890/bad+code
Del: spam -- https://master.php.net/note/delete/96890/spam
Del: non-english -- https://master.php.net/note/delete/96890/non-english
Del: in docs -- https://master.php.net/note/delete/96890/in+docs
Del: other reasons-- https://master.php.net/note/delete/96890
Reject -- https://master.php.net/note/reject/96890
Search -- https://master.php.net/manage/user-notes.php