|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2007-02-18 16:55 UTC] iliaa@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 22:00:01 2025 UTC |
Description: ------------ 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)); $value++; show_eq("$value", json_encode($value)); Expected result: ---------------- 2147483647 == 2147483647 2147483648 == 2147483648 Which shows both functions handling unsigned (or "long") integers. Or: 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.