|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40503 json_encode integer conversion is inconsistent with PHP
Submitted: 2007-02-16 00:47 UTC Modified: 2007-02-18 16:55 UTC
From: mchenryc at gmail dot com Assigned: iliaa (profile)
Status: Closed Package: Output Control
PHP Version: 5CVS-2007-02-16 (snap) OS: Windows XP
Private report: No CVE-ID: None
 [2007-02-16 00:47 UTC] mchenryc at gmail dot com
json_encode converts integers to string in a different way than PHP's integer to string conversion (at least on win32). PHP appears to either handle unsigned values, or use more bits to encode integers, where as json_encode converts them to floats, causing rounding errors.

Reproduce code:
function show_eq($x,$y) {
  echo "$x ". ($x==$y ? "==" : "!=") ." $y\n";

$value = 0x7FFFFFFF; #2147483647;
show_eq("$value", json_encode($value));
show_eq("$value", json_encode($value));

Expected result:
2147483647 == 2147483647
2147483648 == 2147483648

Which shows both functions handling unsigned (or "long") integers.


2147483647 == 2147483647
2147483650 == 2147483650

Which shows both functions converting to a float with less precision.

Actual result:
2147483647 == 2147483647
2147483648 != 2147483650

My code makes use of the "long" or "unsigned" (not sure what's actually happening) ability of PHP for unix timestamps, which I would dearly love to see stay.  The real issue is that the two functions behave in different ways, and should not.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2007-02-18 16:55 UTC]
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
Thank you for the report, and for helping us make PHP better.

PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 08:02:42 2024 UTC