PHP SimpleXMLElement::valid() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
SimpleXML

Definition

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

It is used as the loop condition in a manual loop: while valid() is true, you can read the current child and move on with next(). In normal code, foreach does these checks for you, so it is the preferred way to go through children.

Syntax

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

PHP
$xml->valid();
CallWhat it does
$xml->valid()Returns true while the iterator points to a child element.
$xml->rewind()Moves to the first child, which makes valid() true if a child exists.
while ($xml->valid())The usual loop condition for a manual loop.

Call rewind() first, because the iterator needs a start position. After the last child and one more next(), valid() returns false. Use it as the loop condition to avoid reading past the end.

Library declarations
PHP
SimpleXMLElement::valid(): bool

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
booltrue when the iterator points to a child element, and false when there is no current child.

PHP Examples

Example 1 — Check Before Reading

After rewind(), valid() is true because a first child exists.

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

$xml->rewind();

if ($xml->valid()) {
    echo 'First child: ', $xml->current(), PHP_EOL;
}

How It Works

  1. The fruits element has two fruit children.
  2. rewind() points the iterator to the first one.
  3. valid() returns true, so the if block runs.
  4. current() returns the first child, and echo prints Apple.

Example 2 — Use valid() as the Loop Condition

A while loop runs as long as valid() is true, and stops by itself after the last child.

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

$number = 1;
$xml->rewind();

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

echo 'Finished: ', var_export($xml->valid(), true), PHP_EOL;

How It Works

  1. rewind() points to the first child, so valid() is true when the loop starts.
  2. Each round prints a number and the current child, and then next() moves forward.
  3. After Cherry, next() moves past the last child, so valid() becomes false.
  4. The loop stops, and the last line confirms that valid() is false.

Example 3 — Empty Elements and foreach

An element with no children is never valid, so a loop over it does nothing. foreach handles this case for you.

PHP
<?php
$xml = simplexml_load_string('<list></list>');

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

$count = 0;
foreach ($xml as $child) {
    $count++;
}
echo 'Loop ran: ', $count, ' times', PHP_EOL;

How It Works

  1. The list element has no child elements.
  2. After rewind(), valid() returns false because there is no first child.
  3. foreach checks the same thing in the background, so its body never runs.
  4. The counter stays at 0, which shows that no error happens for an empty element.

Frequently Asked Questions

It tells you whether the iterator currently points to a child element. It returns true while there is a child to read and false when there is none.
The iterator has no position yet. Call rewind() first, and valid() will return true if the element has at least one child.
It returns false after next() moves past the last child. That is the signal that the loop is finished.
No. foreach calls the iterator methods for you, including this check. You need valid() only in manual loops.
It returns false, even after rewind(). A manual loop over that element runs zero times.

Back to SimpleXML Functions

Browse all PHP SimpleXML function tutorials.

SimpleXML functions →

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