W3docs

current()

SimpleXML is a PHP extension that provides a simple and easy-to-use API for working with XML documents. The SimpleXMLElement::current() function is one of the

Introduction

SimpleXML is a PHP extension that provides a simple and easy-to-use API for working with XML documents. The SimpleXMLIterator::current() method is one of the iterator methods provided by the SimpleXMLIterator class. It returns the current SimpleXMLElement object during iteration. In this article, we will discuss how to use this method in PHP.

Syntax

The SimpleXMLIterator::current() method returns the element at the iterator's internal pointer. Its signature is:

public SimpleXMLIterator::current(): SimpleXMLElement

It takes no parameters and returns the SimpleXMLElement (more precisely, a SimpleXMLIterator, which extends SimpleXMLElement) the pointer currently sits on. If the pointer is past the end of the collection, it returns null, which is why current() is normally paired with valid() before use.

current() is one of the five methods that implement PHP's Iterator interface, alongside rewind(), valid(), next(), and key().

How current() behaves inside a foreach loop

PHP's foreach drives an iterator for you: at the start of every pass it calls rewind() (once), then valid(), then current() to populate the loop variable, and finally next() to advance the pointer. So when you call current() inside a foreach body, you get the same element as the loop variable — not the next one.

That means current() is most useful when you control the pointer manually with a while loop, or when you only need the first element after a rewind().

Loading the XML

Before you can iterate, you need a SimpleXMLIterator. There are two common ways to build one:

<?php
// 1. From an XML string (the default — the first argument IS the XML data):
$data = '<books><book><title>PHP Basics</title></book></books>';
$books = new SimpleXMLIterator($data);

// 2. From a file — you MUST pass the third argument `true` (dataIsURL),
//    otherwise the constructor treats the string as raw XML and fails:
$books = new SimpleXMLIterator('books.xml', 0, true);

A frequent mistake is writing new SimpleXMLIterator('books.xml') expecting it to read the file. Without the dataIsURL flag, PHP parses the literal text books.xml as XML and throws a parser error. Use simplexml_load_file() if you prefer a function-style API.

Example: manual iteration with current()

This self-contained example builds the iterator from a string and walks it with rewind(), valid(), current(), and next() so you can see exactly where current() points:

<?php
$data = <<<XML
<books>
  <book><title>PHP Basics</title></book>
  <book><title>Advanced XML</title></book>
</books>
XML;

$books = new SimpleXMLIterator($data);

$books->rewind();              // move pointer to the first <book>
while ($books->valid()) {      // stop when current() would be null
    $current = $books->current();
    echo $current->title . "\n";
    $books->next();            // advance the pointer
}

Output:

PHP Basics
Advanced XML

Example: current() inside foreach

For comparison, the same data with a foreach loop. Here $book already holds the current element, so a separate current() call is redundant:

<?php
$data = '<books>'
      . '<book><title>PHP Basics</title></book>'
      . '<book><title>Advanced XML</title></book>'
      . '</books>';

$books = new SimpleXMLIterator($data);

foreach ($books as $book) {
    // $book === $books->current() at this point in the loop
    echo $book->title . "\n";
}

Output:

PHP Basics
Advanced XML

Conclusion

The SimpleXMLIterator::current() method returns the element at the iterator's internal pointer and is a standard part of PHP's Iterator interface, giving consistent traversal behavior across XML data. Remember that foreach calls current() for you each pass, so inside a loop body it returns the current element rather than the next one. For precise control, drive the pointer yourself with rewind(), valid(), and next(), and read the position label with key(). To learn more about parsing XML, see PHP SimpleXML and the SimpleXML parser.

Practice

Practice
What does SimpleXMLIterator::current() return?
What does SimpleXMLIterator::current() return?
Was this page helpful?