|  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
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.
Block user comment
Status: Assign to:
Bug Type:
From: danielhintermann at yahoo dot de
New email:
PHP Version: OS:


 [2015-01-29 15:29 UTC] danielhintermann at yahoo dot de
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:

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


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

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


        $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>";


Add a Patch

Pull Requests

Add a Pull Request


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

  ' ' == false // => false
This is not a bug, but rather intended and documented behavior.  
[1] <>
 [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] <>
[2] <>
 [2015-05-26 20:25 UTC]
-Status: Open +Status: Verified
 [2015-05-26 20:25 UTC]
Confirmed: <>. (Already fixed in master.)
 [2015-05-27 00:25 UTC]
-Status: Verified +Status: Open
 [2015-05-27 00:25 UTC]
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

According to the comments above, this issue might not qualify as
bug for PHP 5.
 [2018-08-07 16:09 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2018-08-07 16:09 UTC]
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: Thu Jul 25 11:01:30 2024 UTC