PHP SimpleXMLElement::next() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
SimpleXML

Definition

In PHP, SimpleXMLElement::next() moves the iterator forward to the next child element.

It is one of the iterator methods of SimpleXMLElement, together with rewind(), valid(), current(), and key(). You call next() at the end of each round in a manual loop. In normal code, foreach does this for you, so it is the preferred way to go through children.

Syntax

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

PHP
$xml->next();
CallWhat it does
$xml->next()Moves the iterator to the next child element.
$xml->rewind()Moves the iterator back to the first child.
$xml->valid()Checks whether the iterator still points to a child.

next() returns nothing, so there is no value to check. After the last child, the next call to next() leaves the iterator past the end, and valid() becomes false. Use valid() in your loop condition.

Library declarations
PHP
SimpleXMLElement::next(): void

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
voidNothing is returned. Use valid() afterwards to see whether there is a child to read.

PHP Examples

Example 1 — Step Through the Children

Move forward one child at a time and read each one. After the last child, valid() becomes false.

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

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

$xml->next();
echo 'Second: ', $xml->current(), PHP_EOL;

$xml->next();
echo 'Third: ', $xml->current(), PHP_EOL;

$xml->next();
echo 'Valid after last: ', var_export($xml->valid(), true), PHP_EOL;

How It Works

  1. rewind() points the iterator to the first fruit, and current() reads Apple.
  2. Each next() moves the iterator one child forward, so the following current() reads Banana and then Cherry.
  3. The third next() moves past the last child.
  4. valid() now returns false, which means there is nothing left to read.

Example 2 — Skip the First Child

Call next() once before the loop to skip the first child, then loop over the rest.

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

$xml->rewind();
$xml->next();

while ($xml->valid()) {
    echo $xml->current(), PHP_EOL;
    $xml->next();
}

How It Works

  1. rewind() starts at Apple, and the extra next() moves to Banana before the loop begins.
  2. while ($xml->valid()) keeps going as long as the iterator points to a child.
  3. Inside the loop, current() is printed first and then next() moves to the following child.
  4. After Cherry, valid() is false and the loop ends, so Apple is never printed.

Example 3 — Count and Sum with a while Loop

A while loop with next() can count children and add up numbers, though foreach would do it with less code.

PHP
<?php
$xml = simplexml_load_string(
    '<prices><p>5</p><p>12</p><p>3</p></prices>'
);

$count = 0;
$total = 0;

$xml->rewind();
while ($xml->valid()) {
    $count++;
    $total += (int) $xml->current();
    $xml->next();
}

echo 'Count: ', $count, PHP_EOL;
echo 'Total: ', $total, PHP_EOL;

How It Works

  1. The prices element has three p children with the values 5, 12, and 3.
  2. Each round adds 1 to the counter and adds the value of the current child to the total.
  3. next() at the end of each round moves to the next p element.
  4. When valid() turns false, the loop ends and the totals are printed.

Frequently Asked Questions

It moves the iterator of a SimpleXMLElement to the next child element. It is used in manual loops together with rewind(), valid(), and current().
Nothing. Its return type is void. Call valid() afterwards to find out whether the iterator still points to a child.
Use foreach in almost all cases. It calls rewind(), valid(), current(), and next() for you, and the code is shorter and easier to read.
The iterator moves past the end, and valid() returns false. Reading current() at that point is not safe, so check valid() first.
Yes, in a manual loop. rewind() sets the starting position at the first child, and next() then moves forward from there.

Next: SimpleXMLElement::rewind()

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

SimpleXMLElement::rewind() 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