php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68761 filter_var(null) returns false with FILTER_NULL_ON_FAILURE
Submitted: 2015-01-06 22:17 UTC Modified: 2020-07-29 15:49 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: tonyli dot lives at gmail dot com Assigned: cmb (profile)
Status: Closed Package: Filter related
PHP Version: 5.5.20 OS: Ubuntu 12.04
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: tonyli dot lives at gmail dot com
New email:
PHP Version: OS:

 

 [2015-01-06 22:17 UTC] tonyli dot lives at gmail dot com
Description:
------------
Even with the flag on, trying to filter null returns false when it should be still null (as null is not a boolean).

As per the documentation:

If FILTER_NULL_ON_FAILURE is set, FALSE is returned only for "0", "false", "off", "no", and "", and NULL is returned for all non-boolean values.

Similar to: https://bugs.php.net/bug.php?id=67167

Test script:
---------------
var_dump(filter_var(null, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));
var_dump(filter_var(false, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));
var_dump(filter_var(true, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));
var_dump(filter_var('test', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));

Expected result:
----------------
bool(false)
bool(false)
bool(true)
NULL

Actual result:
--------------
NULL
bool(false)
bool(true)
NULL

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-01-15 10:53 UTC] ppasindud at gmail dot com
Works correctly in php-src master
 [2016-09-26 14:47 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2016-09-26 14:47 UTC] cmb@php.net
That issue has been resolved as of PHP 5.4.8, see
<https://3v4l.org/cBDRY>.
 [2020-07-29 14:09 UTC] rihards dot sceredins at creative dot gs
Hey,

It seems that the author of this bug report mixed "expected result" with "actual result".

I just tried this on PHP 7.4.8 and output of the test script mentioned above is:
bool(false)
bool(false)
bool(true)
NULL

Why is false returned for null?
 [2020-07-29 15:11 UTC] cmb@php.net
-Status: Closed +Status: Re-Opened
 [2020-07-29 15:11 UTC] cmb@php.net
> Why is false returned for null?

Because the filter_*() functions are meant to operate on strings,
so values of other types are converted to string first; NULL is
converted to an empty string, and an empty string is supposed to
return FALSE with FILTER_NULL_ON_FAILURE.

Re-opening, since the documentation could be clarified regarding
the string conversion.
 [2020-07-29 15:49 UTC] cmb@php.net
-Status: Re-Opened +Status: Closed
 [2020-07-29 15:49 UTC] cmb@php.net
> […], since the documentation could be clarified regarding the
> string conversion.

Oh, that is already documented[1]:

| Note that scalar values are converted to string internally
| before they are filtered.

[1] <https://www.php.net/manual/en/function.filter-var.php>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 14:01:29 2024 UTC