PHP SimpleXMLElement::current() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
SimpleXML

Definition

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();
CallWhat 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

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
SimpleXMLElementThe child element at the current position.
nullNothing 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.

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;

How It Works

  1. The XML has a fruits root with three fruit children.
  2. rewind() moves the iterator to the first child.
  3. current() returns that first child, and echo prints its text, which is Apple.
  4. next() moves forward by one child, so the next current() call gives Banana.

Example 2 — Loop with valid(), current(), and next()

A for loop can use the iterator methods one by one. The valid() check keeps you from reading past the last child.

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

for ($xml->rewind(); $xml->valid(); $xml->next()) {
    echo $xml->key(), ': ', $xml->current(), PHP_EOL;
}

How It Works

  1. The for loop starts with rewind(), so the iterator points to the first child.
  2. valid() is checked before every round, so the loop stops after the last child.
  3. key() gives the element name and current() gives the child, so each line shows the name and the text.
  4. next() moves to the following child at the end of each round.

Example 3 — The Simpler Way: foreach

In everyday code, foreach does the same job with less code and no risk of forgetting rewind().

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

foreach ($xml->fruit as $fruit) {
    echo $fruit, PHP_EOL;
}

foreach ($xml as $name => $fruit) {
    echo $name, ' = ', $fruit, PHP_EOL;
}

How It Works

  1. foreach ($xml->fruit as $fruit) visits every child named fruit.
  2. PHP rewinds, checks, and moves forward for you, so there is no need to call current() yourself.
  3. foreach ($xml as $name => $fruit) goes through all children and also gives each element name as the key.
  4. Both loops print the same three fruits, which shows why foreach is preferred.

Frequently Asked Questions

It returns the child element that the iterator currently points to. It is one of the methods that make SimpleXMLElement work as an iterator.
No. A foreach loop is the preferred way and it handles the iterator for you. Use current() only when you need to step through the children by hand.
The iterator does not point to a child until you call rewind(). After rewind(), valid() tells you whether there is a first child to read.
It returns a SimpleXMLElement. When you echo it or cast it with (string), you get the text of that element.
In PHP 8, yes. It implements RecursiveIterator, which adds getChildren() and hasChildren() to the other iterator methods.

Next: SimpleXMLElement::getChildren()

SimpleXMLElement::getChildren() returns the child elements of the element that the iterato.

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