PHP SimpleXMLElement::rewind() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
SimpleXML

Definition

In PHP, SimpleXMLElement::rewind() moves the iterator back to the first child element.

It is the starting point of every manual loop over a SimpleXMLElement, and you can also call it to go back to the beginning later. In everyday code, foreach calls rewind() for you, so it is the preferred way to go through children.

Syntax

Call SimpleXMLElement::rewind() with the values you need. Start with this line.

PHP
$xml->rewind();
CallWhat it does
$xml->rewind()Points the iterator to the first child element.
$xml->valid()Checks whether a child exists at the current position, for example right after rewind().
foreach ($xml as $child)The simpler loop that calls rewind() automatically.

After rewind(), valid() is true when the element has at least one child, and current() returns that first child. If the element has no children, valid() stays false after rewind().

Library declarations
PHP
SimpleXMLElement::rewind(): void

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
voidNothing is returned. Call valid() to see whether a first child exists.

PHP Examples

Example 1 — Start Reading from the Beginning

Rewind the iterator, then check that a first child exists before reading it.

PHP
<?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

  1. The fruits element has three fruit children.
  2. rewind() moves the iterator to the first child.
  3. valid() is true because that first child exists.
  4. current() returns the first child, and echo prints its text, which is Apple.

Example 2 — Go Back After Moving Forward

Move through the children, then call rewind() to go back to the first one.

PHP
<?php
$xml = simplexml_load_string(
    '<fruits><fruit>Apple</fruit><fruit>Banana</fruit><fruit>Cherry</fruit></fruits>'
);

$xml->rewind();
$xml->next();
$xml->next();
echo 'After two steps: ', $xml->current(), PHP_EOL;

$xml->rewind();
echo 'After rewind: ', $xml->current(), PHP_EOL;

How It Works

  1. rewind() starts at Apple, and two calls to next() move to Cherry.
  2. current() confirms that the iterator is at Cherry.
  3. A second rewind() puts the iterator back at the first child.
  4. current() now returns Apple again, so you can read the children a second time.

Example 3 — An Element Without Children

When an element has no child elements, there is nothing to rewind to, and valid() stays false.

PHP
<?php
$withChildren = simplexml_load_string('<list><item>One</item></list>');
$empty = simplexml_load_string('<list></list>');

$withChildren->rewind();
echo 'With children: ', var_export($withChildren->valid(), true), PHP_EOL;

$empty->rewind();
echo 'Empty: ', var_export($empty->valid(), true), PHP_EOL;

How It Works

  1. Two small XML strings are loaded: one list with an item, and one empty list.
  2. rewind() on the first list points to its only item, so valid() is true.
  3. rewind() on the empty list has no child to point to.
  4. valid() is false for the empty list, so a loop over it would run zero times.

Frequently Asked Questions

It moves the iterator back to the first child element of the SimpleXMLElement. It is the first step of a manual loop.
No. foreach calls rewind(), valid(), current(), key(), and next() for you in the background.
Nothing breaks. valid() simply returns false after rewind(), so a loop that checks valid() runs zero times.
Nothing. Its return type is void. Use valid() after it to see whether a first child exists.
Avoid it. foreach and the manual methods share the same iterator position, so calling rewind() inside the loop can restart the loop in confusing ways.

Next: SimpleXMLElement::valid()

SimpleXMLElement::valid() checks whether the iterator still points to a child element.

SimpleXMLElement::valid() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful