Edit report at http://bugs.php.net/bug.php?id=51344&edit=1
ID: 51344 Updated by: ahar...@php.net Reported by: pravila at alumni dot calpoly dot edu Summary: FILTER_NULL_ON_FAILURE flag automatically set in filter_input() functions. -Status: Assigned +Status: Wont fix Type: Bug Package: Filter related Operating System: Linux PHP Version: 5.2.13 Assigned To: aharvey New Comment: 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.) A table showing the return value from filter_input() in the different cases follows: | "yes" | "no" | "invalid" | non-existent | No flags | TRUE | FALSE | FALSE | NULL | FILTER_NULL_ON_FAILURE | TRUE | FALSE | NULL | FALSE | 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. Previous Comments: ------------------------------------------------------------------------ [2010-04-11 01:15:01] mats dot lindh at gmail dot com Patch to solve issue has been added. Patch is against current trunk but can probably be applied cleanly against 5.2 and 5.3 too. Issue stems from a simple error where RETURN_NULL(); and RETURN_FALSE; statements seems to have gotten mixed up. Test is also attached in patch, based on the example in the bug report. ------------------------------------------------------------------------ [2010-03-21 18:02:46] pravila at alumni dot calpoly dot edu Description: ------------ * This is different than bug http://bugs.php.net/bug.php?id=41305 * The filter_var() vs. the filter_input() behave differently when using the FILTER_VALIDATE_BOOLEAN filter when the variable/input doesn't exist. More specifically, it seems as if the FILTER_NULL_ON_FAILURE flag is set automatically in the filter_input() function. (Note: same behavior for filter_var_array() vs. filter_input_array()). >From PHPINFO(): filter.default = unsafe_raw filter.default_flags = no value Revision: 1.52.2.39.2.16 Test script: --------------- <?php // example.com/script.php?arg1=yes&arg3=no // 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 unless the FILTER_NULL_ON_FAILURE flag is set... var_dump($input1); // bool(true) var_dump($input2); // NULL var_dump($input3); // bool(false) ?> Expected result: ---------------- As per the documentation, we expect the output of the code above to be: bool(true) bool(false) bool(false) bool(true) bool(false) bool(false) Actual result: -------------- Even though the FILTER_NULL_ON_FAILURE flag is NOT set, we DO get a NULL value in the output: bool(true) bool(false) bool(false) bool(true) NULL bool(false) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=51344&edit=1