ConFoo Montreal 2026: Call for Papers

Voting

: six minus three?
(Example: nine)

The Note You're Voting On

joshduck at gmail dot com
14 years ago
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)

<< Back to user notes page

To Top