|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54973 SimpleXML casts intergers wrong.
Submitted: 2011-06-01 22:18 UTC Modified: 2017-12-26 11:41 UTC
Avg. Score:3.5 ± 0.8
Reproduced:4 of 4 (100.0%)
Same Version:1 (25.0%)
Same OS:2 (50.0%)
From: bphelpsen at gmail dot com Assigned: nikic (profile)
Status: Closed Package: SimpleXML related
PHP Version: 5.3.6 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: bphelpsen at gmail dot com
New email:
PHP Version: OS:


 [2011-06-01 22:18 UTC] bphelpsen at gmail dot com
When an XML node value is an integer SimpleXML always casts it to an int, even if its a float or double.

Test script:
$xml = simplexml_load_string("<xml><number>214748364800</number></xml>");

echo $xml->number . "\n"; // the proper number

$int = $xml->number / 1024 / 1024 / 1024; // initial cast to an int causes problems
echo $int . "\n"; 

$double = (double) $xml->number / 1024 / 1024 / 1024; // hard cast to a double fixes it
echo $double . "\n";

Expected result:

Actual result:


Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-06-02 20:00 UTC]
-Status: Open +Status: Bogus
 [2011-06-02 20:00 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

This has nothing to do with SimpleXML, but rather the fact that a numeric string 
in PHP by default would converted to an integer.
 [2011-06-02 20:05 UTC] bphelpsen at gmail dot com
Then why do I get different results when I run this code:

<?php echo "214748364800" / 1024 / 1024 / 1024; ?>

than when I run the example code.  Shouldn't they return the same value?

Here is an example, with the same results as my local test:
 [2011-06-02 22:14 UTC]
This looks like a bug... SimpleXML's cast_object handler doesn't check for 
overflow when trying to convert a node value to a 
long. $xml->value goes through Zend's add_function and gets passed to 
zendi_convert_scalar_to_number which calls SimpleXML's 
cast_object handler because $xml->number is an object. It then gets chopped into 
LONG_MAX by strtol. An overflow check could 
fix this... but it might break some BC.... 

 [2011-06-03 03:44 UTC]
-Status: Bogus +Status: Analyzed
 [2011-06-03 11:55 UTC]
I remember seeing this before. The problem is that the Zend engine, when doing the add operation, converts any objects it finds to ints. The simplexml extension is merely doing what it's asked, i.e., convert the value of the object to int.

The solution must be either
a) making the Zend Engine default the conversion to a double
b) making the Zend Engine try both a double and an int and compare the two results
c) making the Zend Engine convert the object to a string instead and then convert the string using the usual means (that is, is_numeric_string)
d) change the API so that cast_object can be told to convert the object to either an int or a double, sort of like is_numeric_string
 [2013-07-22 08:33 UTC]
The referenced pull request (#213) has two tests to verify the fix for this bug 
(when and if ready). Please don't forget to use them when commiting the fix.
 [2013-11-06 14:42 UTC] corry at jhstudios dot com
$stdobj= new stdClass;
echo 1+$stdobj->Price;

object(stdClass)#1 (1) { ["Price"]=> string(3) ".53" } 

This shows that the standard class is correctly juggled from a string to a float when added to an integer. This is in contrast to other comments that suggested that the reported behavior is to be expected.
 [2016-09-19 13:23 UTC]
> This shows that the standard class is correctly juggled from a
> string to a float when added to an integer.

Not really. Actually, the object isn't juggled at all, but rather
one of its properties.
 [2017-12-26 11:41 UTC]
-Status: Analyzed +Status: Closed -Assigned To: +Assigned To: nikic
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jul 22 17:01:31 2024 UTC