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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
4 + 5 = ?
Subscribe to this entry?

 
 [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: Fri Mar 29 02:01:30 2024 UTC