# SimpleXML bug

- Thread: https://externals.io/message/4388
- Original archive: https://news-web.php.net/php.internals/4388
- Started by Sterling Hughes on 2003-09-04T10:45:12+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #4388 — Sterling Hughes — 2003-09-04T10:45:12+00:00

URL: https://externals.io/message/4388#4388

Zeev seems to be working on foreach and simplexml, but the above
behaviour is the correct one.  Why should the first loop?  If I did:

<?php
foreach ("hello" as $world) {
	var_dump($world);
}
?>

what would i get?

-Sterling


>   The following script
> 
>     <?php
>     $root = simplexml_load_string('
>     <root>
>       <child />
>     </root>
>     ');
> 
>     foreach ($root->child as $child) {
>         var_dump($child);
>     }
>     ?>
> 
>   does not print anything while
> 
>     <?php
>     $root = simplexml_load_string('
>     <root>
>       <child />
>       <child />
>     </root>
>     ');
> 
>     foreach ($root->child as $child) {
>         var_dump($child);
>     }
>     ?>
> 
>   correctly prints
> 
>     object(simplexml_element)#2 (0) {
>     }
>     object(simplexml_element)#3 (0) {
>     }
> 
> -- 
> Sebastian Bergmann
> http://sebastian-bergmann.de/                   http://phpOpenTracker.de/
> 
> Das Buch zu PHP 5: http://professionelle-softwareentwicklung-mit-php5.de/
-- 
If a 6600 used paper tape instead of core memory, it would use up tape 
at about 30 miles/second. 
  - Grishman, Assembly Language Programming

---

## #4389 — Shane Caraveo — 2003-09-04T23:29:01+00:00

URL: https://externals.io/message/4388#4389 · In reply to: #4388

The problem is one of simplicity ;)
I ran into the same problem a couple days ago.  In the two examples, an 
array of element 'child' is expected.  That array may be one or more 
elements.  SimpleXML turns multiple children of the same element name 
into array's, but a single child is not.  I'm not sure what the right 
answere is for it though, other than conditionaly checking for a single 
child.

There's a part of me that thinks that children should be accessed via a 
function, but then you start getting dom-like:

$root->childNodes()[0]

and it always works.  It also prevents problems with something like:

<root child="test">
   <child>abcd</child>
</root>

Shane

Sterling Hughes wrote:
> Zeev seems to be working on foreach and simplexml, but the above
> behaviour is the correct one.  Why should the first loop?  If I did:
> 
> <?php
> foreach ("hello" as $world) {
> 	var_dump($world);
> }
> ?>
> 
> what would i get?
> 
> -Sterling
> 
> 
> 
> > […]

---

## #4390 — Zeev Suraski — 2003-09-04T23:49:11+00:00

URL: https://externals.io/message/4388#4390 · In reply to: #4388

We can (and I think we should) solve the attribute/child node ambiguity 
using the new array-syntax overloading, i.e., use $root['child'] for the 
attribute, and $root->child for nested elements.  We should definitely stay 
away from having to use functions, either way.

As for the array behavior, it's trickier, we need to think about it.  Using 
an object that can cast itself to an array and implements the 'get' method 
(to fetch its value) may be the right answer.

Zeev

At 02:29 05/09/2003, Shane Caraveo wrote:
>The problem is one of simplicity ;)
>I ran into the same problem a couple days ago.  In the two examples, an 
>array of element 'child' is expected.  That array may be one or more 
>elements.  SimpleXML turns multiple children of the same element name into 
>array's, but a single child is not.  I'm not sure what the right answere 
>is for it though, other than conditionaly checking for a single child.
>
>There's a part of me that thinks that children should be accessed via a 
>function, but then you start getting dom-like:
>
>$root->childNodes()[0]
>
>and it always works.  It also prevents problems with something like:
>
><root child="test">
>   <child>abcd</child>
></root>
>
>Shane
>
>Sterling Hughes wrote:
> > […]
