|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51344 FILTER_NULL_ON_FAILURE flag automatically set in filter_input() functions.
Submitted: 2010-03-21 18:02 UTC Modified: 2010-04-20 06:25 UTC
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: pravila at alumni dot calpoly dot edu Assigned: aharvey (profile)
Status: Wont fix Package: Filter related
PHP Version: 5.2.13 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: pravila at alumni dot calpoly dot edu
New email:
PHP Version: OS:


 [2010-03-21 18:02 UTC] pravila at alumni dot calpoly dot edu
* This is different than bug *

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()).

filter.default = unsafe_raw
filter.default_flags = no value

Test script:

// 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:


Actual result:
Even though the FILTER_NULL_ON_FAILURE flag is NOT set, we DO get a NULL value in 
the output:



bug51344-fix-wrong-return-value-for-null-flag (last revision 2010-04-10 23:13 UTC by mats dot lindh at gmail dot com)

Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-11 01:15 UTC] 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-04-20 02:48 UTC]
-Status: Open +Status: Assigned -Assigned To: +Assigned To: aharvey
 [2010-04-20 06:25 UTC]
-Status: Assigned +Status: Wont fix
 [2010-04-20 06:25 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         |

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.
 [2010-04-20 06:31 UTC]
Automatic comment from SVN on behalf of aharvey
Log: Added explanatory comments to filter_input and filter_input_array to document
why some code that looks intuitively wrong is actually correct. Related to
bug #51344 (FILTER_NULL_ON_FAILURE flag automatically set in filter_input()
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Mon Dec 05 15:05:52 2022 UTC