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

Pull Requests

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: Sat Dec 21 17:01:58 2024 UTC