ID:               32479
 User updated by:  akorthaus at web dot de
 Reported By:      akorthaus at web dot de
 Status:           Closed
 Bug Type:         SimpleXML related
 Operating System: Linux 2.4.28 (Gentoo)
 PHP Version:      5.0.3
 New Comment:

Thank you!

In the manual only casting to (string) is mentioned. If casting to
float is necessary too, I think it should be mentioned in the manual,
because it is not "usual php behaviour". 

btw. I don't understand why casting must be necessary here, because it
is not necessary when using arrays or anything else. I think it's
confusing for many people.


Previous Comments:
------------------------------------------------------------------------

[2005-03-29 11:38:08] [EMAIL PROTECTED]

Works with latest CVS. (you need to cast the stuff to float / string
though)


------------------------------------------------------------------------

[2005-03-29 10:44:46] akorthaus at web dot de

Description:
------------
If I want to calculate with numbers (decimals) from an xml file/string
parsed by simplexml, php does not convert these strings (e.g. "1.2") to
float (1.2), it converts them to integer (1). 

Reproduce code:
---------------
<?php
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<test>
  <num>1.2</num>
  <num>0.2</num>
  <num>0.5</num>
</test>
XML;

$xml = simplexml_load_string($xmlstr);

foreach ($xml->num as $number) {
  echo $number, " * 3 = ", $number * 3, "\n";
  echo "var_dump: ", var_dump($number), "\n";
}

$number_array = array('1.2', '0.2', '0.5');

foreach ($number_array as $number) {
  echo $number, " * 3 = ", $number * 3, "\n";
  echo "var_dump: ", var_dump($number), "\n";
}
?>


Expected result:
----------------
The following loop:

foreach ($xml->num as $number) {
  echo $number, " * 3 = ", $number * 3, "\n";
}

should display:

1.2 * 3 = 3.6
0.2 * 3 = 0.6
0.5 * 3 = 1.5

(as it actually works with $number_array)

Actual result:
--------------
1.2 * 3 = 3
0.2 * 3 = 0
0.5 * 3 = 0




------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=32479&edit=1

Reply via email to