|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-03-21 18:02 UTC] 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) Patchesbug51344-fix-wrong-return-value-for-null-flag (last revision 2010-04-10 23:13 UTC by mats dot lindh at gmail dot com)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 00:00:01 2025 UTC |
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.