php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #5404 String conversion to integer is incorrect
Submitted: 2000-07-06 15:54 UTC Modified: 2001-05-06 14:49 UTC
From: kix at struktuur dot ee Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0.1 OS: Red Hat Linux , kernel 2.2.14-5.
Private report: No CVE-ID: None
 [2000-07-06 15:54 UTC] kix at struktuur dot ee
The code:

$nr = "0x2f";
$n = $nr + 0;
echo "n = $n";

produces the result: 
n = 1.46875

which is obviously incorrect. according to the variable conversion rules, it should be converted to 0.

although it would be really nice if it would be converted to the right value.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-07-06 16:36 UTC] stas at cvs dot php dot net
It's not really PHP. It's C's strtod() function which is to blame. It does very strange tricks when it meets 0x-numbers. 
So, "0x2f" converted by strtod into 1072693248. 
 [2000-07-07 06:57 UTC] joey at cvs dot php dot net
This is a bug, but you may want to note that it works if
$nr is NOT a string...
 [2000-07-07 07:05 UTC] joey at cvs dot php dot net
Also, note that perl has the same problem (for the same
reason, I assume). strtod() is also the culprit in some
localization issues...
 [2000-07-14 21:01 UTC] sterling at cvs dot php dot net
.
 [2000-08-03 21:52 UTC] hholzgra@php.net
so maybe we should add some kind of
strtod-wrapper to get around this libc 'feature'?
 [2000-08-04 01:50 UTC] kix at struktuur dot ee
No, it is not necessary to create a new strtod function, because when php checks if the string is a number or not it explicitly tells strtod, that the string is a base 10 number, instead of letting strtod determine the base itself, which would result in the correct behaviour.

 [2000-08-04 23:36 UTC] hholzgra@php.net
you're talking about strtol() here, not strtod(), aren't you?
 [2000-08-05 04:28 UTC] kix at struktuur dot ee
Yes, of course, sorry about the typo. 

It seems to me that the place where this is done in such a way is in the function is_numeric_string() in the file zend_operators.* 
 [2000-08-05 12:27 UTC] stas@php.net
Yes, strtod makes mess from 0x-numbers (I suspect it being libc bug, but maybe they intended it to be so), so we should catch them in strtol.
 [2001-05-06 14:49 UTC] zeev@php.net
Fixed at last - thanks for the report!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 21:01:31 2024 UTC