|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45023 SimpleXML inconsistent behaviour with multiple xml nodes
Submitted: 2008-05-16 17:56 UTC Modified: 2008-05-16 21:43 UTC
From: norbert_schuetz at bigfoot dot com Assigned:
Status: Not a bug Package: SimpleXML related
PHP Version: 5.2.0 OS: Linux 2.6.18-5-686
Private report: No CVE-ID: None
 [2008-05-16 17:56 UTC] norbert_schuetz at bigfoot dot com
PHP 5.2.0-8+etch11 (cli) (built: May 10 2008 10:46:24) from
Linux 2.6.18-5-686 #1 SMP Sun Aug 12 21:57:02 UTC 2007 i686 GNU/Linux
Simplexml support => enabled
Revision => $Revision: $
Schema support => enabled

Problem description:
For multiple identically named XML nodes the data types are not consistent, results are possibly erratic or at least misleading:

* SimpleXML returns the first array element instead of returning the array itself when accessing the node - although var_dump() reports the node to be an array.

* gettype() reports "object" while var_dump() reports "array".

* processing the node with foreach reports the key to be the node name, not the numeric array id reported by print_r().

Also, for single XML nodes SimpleXML

* gettype() reports "object" while var_dump() reports "array".

* The node content can be accessed as $xml->node as well as $xml->node[0]. If $xml->node is the node's content (a string, that is) $xml->node[0] should return the first character of the string. At least the documentation should note the ambitious behavior.

Reproduce code:
<?xml version='1.0' standalone='yes'?>

$xml=new SimpleXMLElement($xmlstr);
print '1. var_dump($xml->testdata):' ."\n";
print "\n";

print '2. get_type($xml->testdata->single): ';
print gettype($xml->testdata->single) ."\n";
print "\n";

print "3. print \$xml->testdata->single without and with [0]: \n";
print $xml->testdata->single ."\n";
print $xml->testdata->single[0] ."\n";
print "\n";

print '4. var_dump($xml->testdata->single): ';
print "\n";

print '5. get_type($xml->testdata->multiple): ';
print gettype($xml->testdata->multiple) ."\n";
print "\n";

print '6. var_dump($xml->testdata->multiple): ';
print "\n";

print '7. foreach($xml->testdata->multiple ...):' ."\n";
foreach ($xml->testdata->multiple as $key=>$item) {
    print $key .' = ' . $item ."\n";

Expected result:
6. var_dump($xml->testdata->multiple): object(SimpleXMLElement)#4 (1) 
array(3) {
  string(3) "one"
  string(3) "two"
  string(5) "three"

7. foreach($xml->products->multiple ...):
0 = one
1 = two
2 = three

Actual result:
1. var_dump($xml->testdata):
object(SimpleXMLElement)#2 (2) {
  string(4) "test"
  array(3) {
    string(3) "one"
    string(3) "two"
    string(5) "three"

2. get_type($xml->testdata->single): object

3. print $xml->testdata->single without and with [0]:

4. var_dump($xml->testdata->single): object(SimpleXMLElement)#3 (1) {
  string(4) "test"

5. get_type($xml->testdata->multiple): object

6. var_dump($xml->testdata->multiple): object(SimpleXMLElement)#4 (1) {
  string(3) "one"

7. foreach($xml->testdata->multiple ...):
multiple = one
multiple = two
multiple = three


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-05-16 21:43 UTC]
Thank you for taking the time to report a problem with PHP.
Unfortunately you are not using a current version of PHP -- 
the problem might already be fixed. Please download a new
PHP version from

If you are able to reproduce the bug with one of the latest
versions of PHP, please change the PHP version on this bug report
to the version you tested and change the status back to "Open".
Again, thank you for your continued support of PHP.

PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Jun 23 14:01:30 2024 UTC