In PHP, SimpleXMLElement::current() returns the child element that the iterator points to right now.
In PHP 8, SimpleXMLElement implements the RecursiveIterator interface, so it has the iterator methods rewind(), valid(), current(), key(), and next(). For normal code, a foreach loop is the easier and preferred way to go through children. The manual methods are useful when you want to step through the children yourself.
Syntax
Call SimpleXMLElement::current() with the values you need. Start with this line.
PHP
echo $xml->current();
Call
What it does
$xml->current()
Returns the child element at the current iterator position.
echo $xml->current()
Prints the text of the current child element.
foreach ($xml->fruit as $fruit)
The simpler way to visit each child, without calling current() yourself.
Call rewind() first so the iterator points to the first child. Then call valid() to check that a child exists before you call current(). The methods work on the children of the element you call them on.
Library declarations
PHP
SimpleXMLElement::current(): ?SimpleXMLElement
Parameters
Parameter
Description
—
This function takes no parameters.
Return Value
Return Value
Description
SimpleXMLElement
The child element at the current position.
null
Nothing useful is returned when the iterator is not pointing at a child, so check valid() first.
PHP Examples
Example 1 — Read the Current Child
Rewind the iterator, read the first child, move forward, and read the second child.