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();
Call
What 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
Parameter
Description
—
This function takes no parameters.
Return Value
Return Value
Description
bool
true 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.