Example 1 — Start Reading from the Beginning
Rewind the iterator, then check that a first child exists before reading it.
<?php
$xml = simplexml_load_string(
'<fruits><fruit>Apple</fruit><fruit>Banana</fruit><fruit>Cherry</fruit></fruits>'
);
$xml->rewind();
echo 'Valid: ', var_export($xml->valid(), true), PHP_EOL;
echo 'First: ', $xml->current(), PHP_EOL; How It Works
- The fruits element has three fruit children.
- rewind() moves the iterator to the first child.
- valid() is true because that first child exists.
- current() returns the first child, and echo prints its text, which is Apple.

