|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47752 FILTER_VALIDATE_INT doesn't allow "+0" and "-0"
Submitted: 2009-03-23 05:40 UTC Modified: 2009-11-25 10:41 UTC
From: for-bugs at hnw dot jp Assigned: pajoye (profile)
Status: Closed Package: Filter related
PHP Version: 5.2.9 OS: *
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: for-bugs at hnw dot jp
New email:
PHP Version: OS:


 [2009-03-23 05:40 UTC] for-bugs at hnw dot jp
FILTER_VALIDATE_INT doesn't allow "+0" and "-0",
while "0", "+1", and "-1" is valid.

Reproduce code:

var_dump(filter_var("+0", FILTER_VALIDATE_INT));
var_dump(filter_var("-0", FILTER_VALIDATE_INT));

Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2009-03-24 11:46 UTC] php at lvp-media dot com
Probably the easiest fix for this would be to change line 88 of logical_filters.c to allow the character "0" as well, seeing values like -0012 and 0012 get returned as false now as well. Con of this is quite obvious as well however, as numbers prefixed by 0 often get interpreted as octal numbers.

A better fix would be to add a check for it;

--- logical_filters.bak  2009-03-24 12:43:23.000000000 +0100
+++ logical_filters.c    2009-03-24 12:45:09.000000000 +0100
@@ -84,6 +84,12 @@

+       /* allow +0 and -0 */
+       if ((str + 1) == end && *str == '0') {
+               *ret = 0;
+               return 1;
+       }
        /* must start with 1..9*/
        if (str < end && *str >= '1' && *str <= '9') {
                ctx_value = ((*(str++)) - '0');
 [2009-03-24 11:49 UTC]
Right, so this patch is not correct as it would allow octal numbers.
 [2009-03-24 11:50 UTC] php at lvp-media dot com
False, this only allows +0 and -0 by checking the lenght of the string, it verifies that the null is the only character in it.
 [2009-03-24 12:44 UTC]
As far as I remember it is on purpose. 0 cannot be negative or positive.

But if we agree on allowing them again, no problem here.

Can you please provide tests as well (phpt)?
 [2009-03-24 15:03 UTC] peter at lvp-media dot com
Hereby a simple bug47752.phpt file. Tested it on a patched- an an unpatched installation, results as expected. Strictly speaking +0 and -0 are valid integers, even though neither of them is positive or negative.

Peter Beverloo

Bug #47752 (FILTER_VALIDATE_INT doesn't allow "+0" and "-0")
$positive = filter_var ('+0', FILTER_VALIDATE_INT);
$negative = filter_var ('-0', FILTER_VALIDATE_INT);
$zero     = filter_var ('0',  FILTER_VALIDATE_INT);
$octal    = filter_var ('-0123', FILTER_VALIDATE_INT);

var_dump ($positive);
var_dump ($negative);
var_dump ($zero);
var_dump ($octal);

 [2009-03-24 15:15 UTC]
We have double checked again the design choices about -/+ 0, it was on purpose. -0 or +0 are not valid integers.

Sorry, but we have to reject this request.

thanks for your understanding,
 [2009-03-24 15:22 UTC]
Make it a documentation bug instead, it should be documented and will be, will add some other things while being at it :)
 [2009-03-24 16:20 UTC]
The documentation should probably mention that +0 and -0 are valid floats.

var_dump(filter_var("+0", FILTER_VALIDATE_FLOAT));
var_dump(filter_var("-0", FILTER_VALIDATE_FLOAT));

 [2009-11-25 10:41 UTC]
Automatic comment from SVN on behalf of vrana
Log: Numbers +0 and -0 (bug #47752)
 [2009-11-25 10:41 UTC]
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 25 15:01:30 2024 UTC