php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42136 filter_var() fails for "0"
Submitted: 2007-07-29 09:38 UTC Modified: 2007-07-29 10:56 UTC
From: gnupun at yahoo dot com Assigned:
Status: Not a bug Package: Filter related
PHP Version: 5.2.3 OS: Windows XP
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.
Password:
Status:
Package:
Bug Type:
Summary:
From: gnupun at yahoo dot com
New email:
PHP Version: OS:

 

 [2007-07-29 09:38 UTC] gnupun at yahoo dot com
Description:
------------
filter_var() design is broken... the following code always returns FALSE:

filter_var() obviously needs two outputs: the filtered value and a success/failure code.

($value, $errcode) = filter_var(...

Reproduce code:
---------------
$myint = "0";
if (!filter_var($myint, FILTER_VALIDATE_INT)) {
    echo "$myint is not an integer\n";
}
else {
    echo "$myint is an integer\n";
}

Expected result:
----------------
0 is an integer

Actual result:
--------------
0 is not an integer

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-07-29 10:56 UTC] pajoye@php.net
if (!0) will succeed. That's how PHP works. 

You should use:

$filtered = filter_var($myint, FILTER_VALIDATE_INT)

if ($filtered === FALSE)...

There is also an option to return NULL on error.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 13:01:27 2024 UTC