|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62717 BC: in PHP 5.4 empty() reports true for simplexmlelement
Submitted: 2012-08-01 14:37 UTC Modified: 2017-10-24 06:45 UTC
Avg. Score:4.5 ± 0.8
Reproduced:18 of 18 (100.0%)
Same Version:12 (66.7%)
Same OS:8 (44.4%)
From: php at sebastianmendel dot de Assigned: requinix (profile)
Status: Closed Package: SimpleXML related
PHP Version: 5.4.5 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: php at sebastianmendel dot de
New email:
PHP Version: OS:


 [2012-08-01 14:37 UTC] php at sebastianmendel dot de
When laoding XML into SimpleXMLElement and iterating with ->children() empty() reports true for SimpleXMLElement objects.

In PHP 5.3.16 empty() returns false.
In PHP 5.4.5 empty() returns true.

Test script:
$xml = <<<EOT
<?xml version="1.0" ?>

$sxml = new SimpleXMLElement($xml);

foreach ($sxml->children() as $xmlKey => $xmlNode) {
        empty($xmlNode), (string) $xmlNode, $xmlNode->asxml()

$xmlNode = new SimpleXMLElement('<X>501936634a721</X>');
    empty($xmlNode), (string) $xmlNode, $xmlNode->asxml()

Expected result:
string(7) "6098881"
string(14) "<X>6098881</X>"
string(13) "501936634a721"
string(43) "<?xml version="1.0"?>

Actual result:
string(7) "6098881"
string(14) "<X>6098881</X>"
string(13) "501936634a721"
string(43) "<?xml version="1.0"?>\n<X>501936634a721</X>\n"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-12 11:47 UTC] jozef at react dot com
This bug was introduced in 5.4.0, as can be seen on:

for the following test:

$string = <<<XML

$xml = new SimpleXMLElement($string);
$result = $xml->xpath('/a/b');

foreach ($result as $r)
    if (!empty($r))
        echo 'failure';
 [2012-12-12 14:18 UTC]
-Assigned To: +Assigned To: and
 [2012-12-12 14:22 UTC]
hey, acurioso, do you have time to look at this?

I assume this is introduced in

 [2012-12-12 14:22 UTC]
-Assigned To: and +Assigned To: acurioso
 [2013-05-31 10:22 UTC] ferca23 at gmail dot com
I don't know if is exactly the same bug, but I have a similar problem.

The empty function is returning true when accessing the sub-elements using the main object, and is returning false when accessing the sub-elements using the variable containing the sub-element.

Example script:

$xmlstr = '<?xml version="1.0" standalone="yes"?>

$movies = new SimpleXMLElement($xmlstr);

$subelement = $movies->movie->title;

echo "Accesing normal: " . PHP_EOL;
echo var_dump($movies->movie->title) . PHP_EOL;
echo var_dump(empty($movies->movie->title)) . PHP_EOL;

echo "Accesing subelement: " . PHP_EOL;
echo var_dump($subelement) . PHP_EOL;
echo var_dump(empty($subelement)) . PHP_EOL;
 [2013-07-19 09:26 UTC] sviroos at gmail dot com
I guess my problem is related to this bug:

$xmlString = '<?xml version="1.0" ?><root><node>not empty</node><node></node><node attr="attr"></node></root>';
$xmlObject = new SimpleXMLElement($xmlString);
echo $xmlString;
echo $xmlObject->asXML();

Expected result:
  <node>not empty</node>
  <node attr="attr"></node>

Actual result:
  <node>not empty</node>
    <node attr="attr"></node>
 [2017-10-24 05:59 UTC]
-Status: Assigned +Status: Open -Assigned To: acurioso +Assigned To:
 [2017-10-24 06:45 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: requinix
 [2017-10-24 06:45 UTC]
According to this is fixed since 5.5.27/5.6.11.

Original code doesn't seem to reproduce.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jul 22 18:01:30 2024 UTC