note 100162 added to class.simplexmlelement
| From: | joshduck at gmail dot com | Date: | Wed, 29 Sep 2010 00:00:14 +0000 |
| Subject: | note 100162 added to class.simplexmlelement | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172792@lists.php.net to get a copy of this message | ||
You should cast elements to a float (or even string) if you plan on using them in addition,
multiplication, etc. If you don't then PHP will incorrectly treat the node values as an
integer.
<?php
$obj = new SimpleXMLElement('<root>
<a>1.9</a>
<b>1.9</b>
</root>');
var_dump($obj->a + $obj->b);
var_dump((float)$obj->a + (float)$obj->b);
var_dump((string)$obj->a + (string)$obj->b);
?>
The first line gives: int(2)
The second and third give the expected result: float(3.8)
----
Server IP: 117.55.229.1
Probable Submitter: 203.201.149.10
----
Manual Page -- http://www.php.net/manual/en/class.simplexmlelement.php
Edit -- https://master.php.net/note/edit/100162
Del: integrated -- https://master.php.net/note/delete/100162/integrated
Del: useless -- https://master.php.net/note/delete/100162/useless
Del: bad code -- https://master.php.net/note/delete/100162/bad+code
Del: spam -- https://master.php.net/note/delete/100162/spam
Del: non-english -- https://master.php.net/note/delete/100162/non-english
Del: in docs -- https://master.php.net/note/delete/100162/in+docs
Del: other reasons-- https://master.php.net/note/delete/100162
Reject -- https://master.php.net/note/reject/100162
Search -- https://master.php.net/manage/user-notes.php