php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42780 Invalid type casting ?
Submitted: 2007-09-27 16:51 UTC Modified: 2007-10-23 11:35 UTC
Votes:7
Avg. Score:3.9 ± 1.0
Reproduced:5 of 6 (83.3%)
Same Version:0 (0.0%)
Same OS:1 (20.0%)
From: lordi at msdi dot ca Assigned: rrichards (profile)
Status: Wont fix Package: SimpleXML related
PHP Version: 5.2.4 OS: Freebsd + windows
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2007-09-27 16:51 UTC] lordi at msdi dot ca
Description:
------------
Calculations on values got from a simplexml object dont work as expected.

String values looking as float seems to be casted as integers

Reproduce code:
---------------
<?php

	$objXML = new SimpleXMLElement('<test x="-123.45"></test>');
	
	//Shows correctly
	echo $objXML['x']."\n";
	
	//We loose the decimals
	echo $objXML['x'] + $objXML['x']."\n";
	
	//This works if we cast the amounts
	echo (float)$objXML['x'] + (float)$objXML['x']."\n";
	
	//Calculated on a string, no problem
	echo "-123.45" + "-123.45";
?>

Expected result:
----------------
-123.45
-246.9
-246.9
-246.9

Actual result:
--------------
-123.45
-246
-246.9
-246.9

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-10-23 11:35 UTC] rrichards@php.net
The behavior is defined by the engine not the extension. When performing mathematical operations on objects, they are treated as integers. It is up to the user to cast the object to the appropriate type to maintain proper precision.
 [2010-08-15 19:41 UTC] rowan dot collins at gmail dot com
Hi,

I was about to report this bug myself, and am surprised at the resolution, as this seems to be very unhelpful and unexpected behaviour.

Consider the following minimal test case:
<?php
$x = simplexml_load_string('<x>2.5</x>');
var_dump($x*1); // int(2)
var_dump((string)$x*1) // float(2.5)
?>

Is there any other value of $x that would create the behaviour shown with those 2 var_dump() calls?

You say that "when performing mathematical operations on objects, they are treated as integers", but I'm not aware of any other object which interprets its contents to cast to integer in this way. Certainly any user-defined class will simply become int(1) in this context, so SimpleXML is *attempting* to perform extra "magical" functionality here.

I discovered this in a production application which was dropping pennies from prices returned in XML by a supplier - a non-trivial bug. If the implicit cast had resulted in int(1), like any other object, this would have been ridiculously obvious; but since SimpleXML('<x>42.0</x>') appears to act as float(42.0), there was no reason to suspect that SimpleXML('<x>42.5</x>') would not act as float(42.5).

I also note that if $x in the example were the string '2.5', var_dump($x*1) would result in float(2.5) as expected, so it is not true that the engine has already decided that the variable is in int context.

If this is a limitation of the engine, then that should be raised as a dependency of fixing this bug, not used as an excuse to dismiss it.

Many thanks for all your efforts,
Rowan Collins
 [2010-09-29 02:04 UTC] joshduck at gmail dot com
This behaviour is so broken it's not funny. How can silently discarding data be acceptable? My values were overflowing as ints, meaning the results I was getting were off by an order of magnitude.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Oct 27 13:01:24 2020 UTC