php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #41662 SimpleXML->children() misleading result
Submitted: 2007-06-12 07:36 UTC Modified: 2010-01-07 12:25 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: kenashkov at gmail dot com Assigned: kalle (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.2.4RC3-dev OS: Fedora Core 4
Private report: No CVE-ID: None
 [2007-06-12 07:36 UTC] kenashkov at gmail dot com
Description:
------------
Testing an SimpleXMLElement object for children is unconsistent.
Here is an example:
--------
$str = '<rootnode><subnode></subnode></rootnode>';
$x = new SimpleXMLElement($str);
if($x->subnode->children())
    print 'yes';
    else
        print 'no';
---------
will print 'no';

If the
$str='<rootnode><subnode><newnode></newnode></subnode></rootnode>';
it will print yes.
But the same will happen if the subnode has an attribute like:
$str = '<rootnode><subnode id="2"></subnode></rootnode>';

But if we use foreach($x->subnode->children() as $key=>$value)
in the latter example we will not get anything (which is correct).
I think is wrong the children() method to return object when there are no child objects and the node has attributes (because for querying the attributes we have the attributes() method).
This was discussed in php-dev list - http://marc.info/?l=php-dev&m=118001203709813&w=2

If this is not a bug, I think a note regarding this behaviour must be added in the docs.

Reproduce code:
---------------
$str = '<rootnode><subnode id="2"></subnode></rootnode>';
$x = new SimpleXMLElement($str);
if($x->subnode->children())
    print 'yes';
    else
        print 'no';

Expected result:
----------------
no

Actual result:
--------------
yes

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-17 02:37 UTC] vrana@php.net
There's no reason why a node without subnode should return anything in its children(). Especially if there is attributes() method returning attributes. Please fix the sources.
 [2007-08-19 18:37 UTC] kenashkov at gmail dot com
Still the same result with 5.2.4RC3-dev.
 [2007-08-19 20:08 UTC] jani@php.net
This is how SimpleXML is (not so simple). Use count() like was suggested on the mailing list. Reclassified as documentation bug.
 [2010-01-07 12:25 UTC] svn@php.net
Automatic comment from SVN on behalf of kalle
Revision: http://svn.php.net/viewvc/?view=revision&revision=293214
Log: Fixed bug #41662 (SimpleXML->children() misleading result)
 [2010-01-07 12:25 UTC] kalle@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 [2020-02-07 06:09 UTC] phpdocbot@php.net
Automatic comment on behalf of kalle
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=446e64f854318d802ccad04a09032a7aaab62582
Log: Fixed bug #41662 (SimpleXML-&gt;children() misleading result)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 14 21:01:33 2025 UTC