next()
Learn how PHP next() advances an array's internal pointer, what it returns, the falsy-value gotcha, and how it pairs with reset(), current(), and prev().
The PHP next() Function
Every PHP array carries an internal pointer that marks one element as the "current" one. The next() function moves that pointer forward by one position and returns the value it lands on. It is part of a small family of pointer functions — reset(), current(), prev(), and end() — that let you walk an array step by step without writing your own index counter. This guide explains exactly what next() returns, how it changes the pointer, and the edge cases that catch people out.
Syntax and return value
next(array &$array): mixedThree details flow from that signature:
- The array is passed by reference (
&$array).next()does not hand you a new array — it mutates the internal pointer of the array you pass in. - It returns the value of the next element, not the current one. To read the element under the pointer without moving it, use
current(). - At the end of the array it returns
falseand parks the pointer past the last element. Every further call also returnsfalse.
When a script first touches an array, the pointer sits on the first element. So the very first next() already returns the second element — a frequent source of off-by-one mistakes.
A basic next() example
The snippet below reads the first element with current(), then steps forward twice. The last call walks off the end and returns false:
The output is:
apple
banana
cherry
bool(false)Walking an array with next()
A common pattern is to call reset() first (to be sure the pointer is at the front), print the first element, then loop with next() until it returns false:
This prints:
red
green
blueNote the strict !== false comparison — the next section explains why a loose check is dangerous.
The falsy-value gotcha
next() returns false at the end of the array, but it also returns false whenever an element's real value is false, 0, "", or null. A loose condition like while (next($array)) therefore stops the moment it meets one of those values:
Because next() returns 0 for the second element, the loop body never runs — the output is just:
doneFor arrays that may hold falsy values, prefer foreach (which ignores the internal pointer entirely) or check key(), which returns null only at the genuine end of the array.
How next() relates to the other pointer functions
next() rarely works alone. It pairs with reset(), current(), prev(), end(), and key() to give you full manual control over an array's cursor:
| Function | Moves the pointer? | Returns |
|---|---|---|
current() | No | Element under the pointer |
next() | Forward one | New current element (or false at the end) |
prev() | Back one | New current element (or false before the start) |
reset() | To the first | First element |
end() | To the last | Last element |
key() | No | Key of the current element (or null past the end) |
The example below uses end() and prev() together with next() to show how the pointer can move in both directions during one pass:
Output:
30
20
30
10When to use next()
For plain iteration over every element, a foreach loop is clearer and safer — it never trips over falsy values. Reach for next() only when you genuinely need explicit, step-by-step control of where the pointer sits, such as peeking ahead while still mid-loop.
Conclusion
The next() function advances an array's internal pointer one step forward and returns the value it lands on, returning false once it runs past the last element. Because it depends on the internal pointer and signals the end with false, it works hand in hand with reset(), current(), and prev() — just remember the falsy-value trap, where a loose loop condition stops too soon. For everyday iteration prefer foreach; choose next() when you need precise control over the array cursor.