|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2020-12-09 13:14 UTC] nikic@php.net
[2020-12-09 13:58 UTC] cmb@php.net
-Status: Open
+Status: Verified
-Type: Bug
+Type: Documentation Problem
[2020-12-09 13:58 UTC] cmb@php.net
[2020-12-09 14:33 UTC] gilperon at gmail dot com
[2020-12-10 03:10 UTC] Dygear at gmail dot com
[2020-12-10 11:30 UTC] cmb@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 09 13:00:01 2025 UTC |
Description: ------------ On versions of PHP prior to 8.0 when you use `simplexml_load_string` you could access the values with no problems and assign them to other functions. However on PHP 8.0 the values are of type SimpleXMLObject and not of string, float... You can see this in action running the pretty simple test script. You will see the first `var_dump`, instead of showing `float(46.50)` or `string(46.50)` it shows: object(SimpleXMLElement)#4 (1) { [0]=> string(5) "46.50" } Of course, it's pretty easy to convert this to a string or float using (string)$x or (float)$x HOWEVER this didnt happen on other versions of PHP. And the real problem arises when you try to use a function like `number_format` that expects a number to work, however it's not getting a number and fails. For some strange reason functions like `round` work just fine without converting the SimpleXMLElement object, however `number_format` has problems dealing with it. Maybe `round` casts the parameter to a numeric value and `number_format` does not. Anyway, I think that if this is an expected result it should be mentioned on migration guide/incompatibiliy notes. Test script: --------------- $xml_retorno = simplexml_load_string('<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?><transaction><date>2020-12-09T09:20:51.000-03:00</date><code>EACDE7D2-EBD5-4F69-B760-640A5EFDC6A6</code><type>1</type><status>3</status><lastEventDate>2020-12-09T09:23:01.000-03:00</lastEventDate><paymentMethod><type>1</type><code>102</code></paymentMethod><grossAmount>46.50</grossAmount><discountAmount>0.00</discountAmount><feeAmount>2.72</feeAmount><netAmount>43.78</netAmount><extraAmount>0.00</extraAmount><escrowEndDate>2020-12-23T01:00:00.000-03:00</escrowEndDate><installmentCount>1</installmentCount><itemCount>1</itemCount><items><item><id>Prestação de serviço digital</id><description>XCHCU7</description><quantity>1</quantity><amount>46.50</amount></item></items><sender><name>GGGG</name><email>corporativo@vvvv.agr.br</email><phone><areaCode>16</areaCode><number>981746872</number></phone></sender><shipping><address><street>Rua Mariana Amaral</street><number>138</number><complement></complement><district>Lagoinha</district><city>SAO XXXDO XXX</city><state>MG</state><country>BRA</country><postalCode>445333</postalCode></address><type>3</type><cost>0.00</cost></shipping></transaction>'); var_dump($xml_retorno -> items -> item[0] -> amount); echo number_format($xml_retorno -> items -> item[0] -> amount,2); Expected result: ---------------- Return the value as string/float... Actual result: -------------- It return the value as a XML object.