Categories
php

print_r in PHP-5.2.6 Doesn’t Print the Whole SimpleXML Object

There’s an interesting SimpleXML related bug in PHP 5.2.6 . print_r is unable to print all the contents of a SimpleXML object. The bug is reported on the php.net site.

The reproduction of the code is below:

Reproduce code:
---------------
<?php
$xmlstr=<<<EOXML
<?xml version='1.0' standalone='yes'?>l;
<products>
    <product order_id="0001"></product>
    <product order_id="0002">PHP book</product>
    <product order_id="0003">
        <name>PHP book</name>
    </product>
</products>
EOXML;

$xml=new SimpleXMLElement($xmlstr);
print_r($xml);
?>

Expected result:
----------------
@attributes array for second item as well

Actual result:
--------------
SimpleXMLElement Object
(
    [product] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [order_id] => 0001
                        )

                )

            [1] => PHP book
            [2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [order_id] => 0003
                        )

                    [name] => PHP book
                )

        )

)

[13 May 1:29pm UTC] felipe@php.net

The attribute can be accessed, however toString really doesn't show it.

print_r($xml->product[1]);

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [order_id] => 0002
        )

    [0] => PHP book
)

It’s been pretty interesting going through the code in php-5.2.6/ext/simplexml . A note in the README file provides a clue:

“Due to engine problems it is currently not possible to access a subelement by index 0: $object->property[0].”

One reply on “print_r in PHP-5.2.6 Doesn’t Print the Whole SimpleXML Object”

What does “[@attributes]” mean: specifically: what is the significance of the “@” character?
Not seen this notation. Cannot access xmlElement attributes directly; have to say $arr=$xml->attributes() then, work your way through $arr.

Basically, am flummoxed about “@attributes.

Comments are closed.