php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #32479 number-strings in xml are converted to int, not float
Submitted: 2005-03-29 10:44 UTC Modified: 2005-03-29 12:47 UTC
From: akorthaus at web dot de Assigned:
Status: Closed Package: SimpleXML related
PHP Version: 5.0.3 OS: Linux 2.4.28 (Gentoo)
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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: akorthaus at web dot de
New email:
PHP Version: OS:

 

 [2005-03-29 10:44 UTC] 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



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-03-29 11:38 UTC] sniper@php.net
Works with latest CVS. (you need to cast the stuff to float / string though)

 [2005-03-29 12:47 UTC] akorthaus at web dot de
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 10 22:01:32 2024 UTC