php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68946 simplexml_load_string() result depends on formatting of xml string
Submitted: 2015-01-29 15:29 UTC Modified: 2018-08-07 16:09 UTC
From: danielhintermann at yahoo dot de Assigned: cmb (profile)
Status: Closed Package: SimpleXML related
PHP Version: 5.6.5 OS: Windows
Private report: No CVE-ID: None
 [2015-01-29 15:29 UTC] danielhintermann at yahoo dot de
Description:
------------
First, the bug could also be triggered on PHP version 5.3.28 on windows.
Secondly, it could be related to the issue Bug #61335.

Depending on formatting of a xml string the comparing with '== false' gives different result. It is clear that the === operator should be used, however, the == operator still should give consistent results!

Test script:
---------------
<?php

class TestComparisonOperator extends PHPUnit_Framework_TestCase {
    public function testWithoutSpaceFormat() {
        $oXml = simplexml_load_string($this->getXmlNoFormatting());

        var_dump($oXml);

        $this->assertFalse($oXml == false);
   }

    public function testWithSpaceFormat() {
        $oXml = simplexml_load_string($this->getXmlWithSpaceFormat());

        var_dump($oXml);

        $this->assertFalse($oXml == false);
    }

    private function getXmlNoFormatting()
    {
        return "<?BrightCloud version=bcap/1.1?><bcap><seqnum>1</seqnum></bcap>";
    }

    private function getXmlWithSpaceFormat()
    {
        // look at the space after bcap tag!
        return "<?BrightCloud version=bcap/1.1?><bcap> <seqnum>1</seqnum></bcap>";
    }
}



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-01-29 15:35 UTC] danielhintermann at yahoo dot de
Could be a duplicate of #68507.
 [2015-01-29 19:37 UTC] cmbecker69 at gmx dot de
The comparison with == false causes $oXml to be cast to a scalar
by calling it's __toString() method. The behavior of
SimpleXmlElement::__toString() is documented as[1]:

| Returns text content that is directly in this element. Does not
| return text content that is inside this element's children.

So basically you have

  '' == false // => true
  
vs.

  ' ' == false // => false
  
This is not a bug, but rather intended and documented behavior.  
  
[1] <http://php.net/manual/en/simplexmlelement.tostring.php>
 [2015-01-29 20:21 UTC] cmbecker69 at gmx dot de
I have to correct myself. According to the PHP language
specification[1], comparing an object and a boolean for equality
causes the object to be converted to boolean. Apparently, this is
not documented in the PHP manual. Anyhow, conversion to boolean is
documented in the PHP manual[2], and all (PHP 5) objects are
converted to true, except "SimpleXML objects created from empty
tags". Unfortunately, this description is confusing, at best.

[1] <https://github.com/php/php-langspec>
[2] <http://php.net/manual/en/language.types.boolean.php#language.types.boolean.casting>
 [2015-05-26 20:25 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2015-05-26 20:25 UTC] cmb@php.net
Confirmed: <http://3v4l.org/c8RMA>. (Already fixed in master.)
 [2015-05-27 00:25 UTC] cmb@php.net
-Status: Verified +Status: Open
 [2015-05-27 00:25 UTC] cmb@php.net
After further investigation I found that this issue is neither
related to bug #62693, bug #66084 nor bug #61335. Actually, the
different behavior is caused by PHP 5 converting the
SimpleXMLElement object to string, but PHP 7 is converting to
bool.

According to the comments above, this issue might not qualify as
bug for PHP 5.
 [2018-08-07 16:09 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2018-08-07 16:09 UTC] cmb@php.net
Since PHP 5 is out of active support, and the issue has been fixed
as of PHP 7.0.0, this ticket can be closed.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 01:01:28 2024 UTC