|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2003-11-24 07:36 UTC] bart at mediawave dot nl
Description:
------------
This bug/feature request has some relation with bug: #25640.
I've loaded XML into a simpleXML object. SimpleXML currently loads empty tags (e.g. <tag />) as empty SimpleXML objects.
With the SimpleXML object I wanted to use the following code to find out whether a tag has child tags or not:
if (is_object($SimpleXMLObjectNode)) {
// $node has child tags
Unfortunately this doesn't work very well since this code will think that empty tags have child tags too. (Since empty tags are loaded as objects)
Therefore I thought it would be a nice feature to be able to find out whether an object is empty or not. Something like:
if (empty($object)) {
// Object is empty
}
Reproduce code:
---------------
<?php
$xml = '<wrapper><foo></foo><bar>s2</bar><bar>s3</bar></wrapper>';
$t = simplexml_load_string($xml);
print_r($t);
if (empty($t->foo)) {
echo 'Tag is empty';
} else {
echo 'Tag has contents';
}
?>
Expected result:
----------------
Tag is empty
Actual result:
--------------
Tag has contents
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 27 05:00:01 2025 UTC |
simplexml_element Object ( [foo] => simplexml_element Object ( ) [bar] => Array ( [0] => s2 [1] => s3 ) ) [foo] looks empty to me? Or maybe there are private properties that I can't see? If so, should empty() return true if an object only has private properties? How else can we tell if an SimpleXML object represents an empty tag?