W3docs

next()

Learn how PHP's next() advances an array's internal pointer, returns the next element or false at the end, and pairs with reset(), current(), and prev().

Introduction

Every PHP array keeps an internal pointer that marks one element as "current". A small family of functions move that pointer without writing a loop yourself: reset(), current(), prev(), end(), and next(). This article focuses on next() — what it returns, how it changes the pointer, and the edge cases that trip people up.

What the next() function does

next() advances the array's internal pointer by one position and returns the value of the element it lands on. The signature is:

next(array &$array): mixed

A few things follow from that signature:

  • The array is passed by reference (&$array). next() does not return a new array — it mutates the pointer of the array you pass in.
  • It returns the next element's value, not the current one. If you want the element under the pointer without moving it, use current().
  • At the end of the array it returns false and leaves the pointer parked past the last element. Calling next() again keeps returning false.

When a script starts, the pointer is on the first element, so the very first next() already returns the second element — not the first. This is the single most common source of "off-by-one" confusion with next().

Basic example

<?php

$fruits = ['apple', 'banana', 'cherry'];

echo current($fruits) . "\n"; // pointer starts on the first element
echo next($fruits) . "\n";    // move forward, return the second
echo next($fruits) . "\n";    // move forward, return the third
var_dump(next($fruits));      // past the end -> false

This outputs:

apple
banana
cherry
bool(false)

current() reads the first element without moving the pointer, then each next() steps forward one element. The final call walks off the end of the array and returns false.

Walking an array with next()

A common pattern is to start with reset() (to be sure the pointer is at the front) and loop with next() until it returns false:

<?php

$colors = ['red', 'green', 'blue'];

reset($colors);
echo current($colors) . "\n"; // first element

while (($color = next($colors)) !== false) {
    echo $color . "\n";
}

Output:

red
green
blue

Note the !== false (strict comparison). That matters — see the next section.

The "falsy value" gotcha

next() returns false at the end of the array, but it also returns false if the actual value of an element is false, 0, "", or null. A loose check like while (next($array)) will stop early the moment it hits one of those values:

<?php

$data = ['a', 0, 'b'];
reset($data);

// WRONG: stops at the 0, never reaches 'b'
while ($value = next($data)) {
    echo $value . "\n";
}
echo "---\n";

This prints only:

---

The loop ends immediately because next() returns 0 (the second element), which is falsy. For arrays that may contain falsy values, prefer foreach, which sidesteps the internal pointer entirely, or use key() to detect the real end:

<?php

$data = ['a', 0, 'b'];
reset($data);

do {
    echo current($data) . "\n";
} while (next($data) !== null && key($data) !== null);

In practice, a plain foreach is the right tool for iterating over every element; reach for next() only when you genuinely need manual, step-by-step control of the pointer.

FunctionMoves pointer?Returns
current()NoElement under the pointer
next()Forward oneNew current element (or false at the end)
prev()Back oneNew current element (or false before the start)
reset()To firstFirst element
end()To lastLast element
key()NoKey of the current element (or null past the end)

Combining next() and prev() lets you peek ahead and step back during a single pass over an array.

Conclusion

The next() function advances an array's internal pointer one step forward and returns the value it lands on, or false once it runs off the end. Because it relies on the internal pointer and signals the end with false, it pairs naturally with reset(), current(), and prev() — but watch out for arrays containing falsy values, where a loose loop condition will stop too soon. For straightforward iteration, prefer foreach; use next() when you need explicit control over where the pointer sits.

Practice

Practice
What does the PHP 'next()' function do according to the information provided on the webpage?
What does the PHP 'next()' function do according to the information provided on the webpage?
Was this page helpful?