Bug #80499 [NEW]: simplexml_load_string does not work as on PHP 7.X

From: Date: Wed, 09 Dec 2020 13:06:55 +0000
Subject: Bug #80499 [NEW]: simplexml_load_string does not work as on PHP 7.X
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-230954@lists.php.net to get a copy of this message
From:             gilperon at gmail dot com
Operating system: ANY
PHP version:      8.0.0
Package:          *XML functions
Bug Type:         Bug
Bug description:simplexml_load_string does not work as on PHP 7.X

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.

-- 
Edit bug report at https://bugs.php.net/bug.php?id=80499&edit=1
-- 
Fix committed:                    https://bugs.php.net/fix.php?id=80499&r=fixed
Fixed in release:                 https://bugs.php.net/fix.php?id=80499&r=alreadyfixed
Need backtrace:                   https://bugs.php.net/fix.php?id=80499&r=needtrace
Need Reproduce Script:            https://bugs.php.net/fix.php?id=80499&r=needscript
Try newer version:                https://bugs.php.net/fix.php?id=80499&r=oldversion
Not developer issue:              https://bugs.php.net/fix.php?id=80499&r=support
Expected behavior:                https://bugs.php.net/fix.php?id=80499&r=notwrong
Not enough info:                  https://bugs.php.net/fix.php?id=80499&r=notenoughinfo
Submitted twice:                  https://bugs.php.net/fix.php?id=80499&r=submittedtwice
register_globals:                 https://bugs.php.net/fix.php?id=80499&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=80499&r=phptooold
Daylight Savings:                 https://bugs.php.net/fix.php?id=80499&r=dst
IIS Stability:                    https://bugs.php.net/fix.php?id=80499&r=isapi
Install GNU Sed:                  https://bugs.php.net/fix.php?id=80499&r=gnused
Floating point limitations:       https://bugs.php.net/fix.php?id=80499&r=float
No Zend Extensions:               https://bugs.php.net/fix.php?id=80499&r=nozend
MySQL Configuration Error:        https://bugs.php.net/fix.php?id=80499&r=mysqlcfg


Thread (1 message)

  • gilperon at gmail dot com
« previous php.bugs (#230954) next »