Signature
prev(array|object &$array): mixedPurpose
Every PHP array carries an internal pointer, a hidden position marker that functions such as current(), next(), reset(), end() and key() read or move. prev() moves that marker one element toward the beginning of the array and returns the value now at that position. It is the mirror image of next().
The pointer is useful when you need to step through an array manually, for example to go back and forth between neighbouring entries, rather than loop over it in one pass.
Signature
Parameters and defaults
$array: the array whose pointer should move. It is taken by reference, so you must pass a variable, not a literal or an expression. The function changes the pointer stored in that variable. There are no other parameters and no defaults.
The signature still accepts objects, but the manual's changelog says that calling it on objects has been deprecated since PHP 8.1. If you have an object, convert it first with get_mangled_object_vars(), or use a class that implements Iterator, such as ArrayIterator.
Return value and failure cases
prev() returns the element value at the new position. When there is no previous element, it returns false. Moving before the first element leaves the pointer in an invalid position, and the same false is returned for an empty array.
Because the return type is mixed, false is ambiguous: an array may legitimately contain a false element (or another falsy value such as 0 or ""). Always compare with ===, and when the distinction matters, check that key() is not null.
Example 1: stepping backwards
The pointer starts on the first element. Here we advance it twice, then walk back until we fall off the start.
$transport = ['foot', 'bike', 'car', 'plane'];
next($transport);
next($transport);
echo current($transport), "\n";
echo prev($transport), "\n";
echo prev($transport), "\n";
var_dump(prev($transport));car bike foot bool(false)
The last call has no earlier element, so it yields false.
Example 2: telling a false element from the start
When an array holds false values, the return value alone cannot tell you whether you moved onto such an element or ran off the beginning. key() can.
$flags = [true, false, true];
end($flags);
$value = prev($flags);
echo json_encode($value), "\n";
echo json_encode(key($flags)), "\n";
prev($flags);
var_dump(prev($flags));
echo json_encode(key($flags)), "\n";false 1 bool(false) null
The first prev() lands on the real false at key 1, so key() reports 1. After moving past the start, key() is null.
Example 3: reverse walk with a key check
A reliable backward loop uses end() to position the pointer and key() to decide when to stop. This works even if some values are falsy, such as the 0 below.
$items = ['a' => 10, 'b' => 0, 'c' => 30];
end($items);
$result = [];
while (key($items) !== null) {
$result[key($items)] = current($items);
prev($items);
}
echo json_encode($result), "\n";{"c":30,"b":0,"a":10}
A loop written as while ($v = prev($items)) would stop early at the 0, which is a classic bug.
Edge cases
- Pointer already at the start:
prev()returnsfalseand the pointer becomes invalid. Furthernext()orprev()calls will not bring it back; callreset()orend()to reposition it. - Empty array: returns
falseimmediately. - Copies: assigning an array to another variable copies the array value together with its pointer position, and each variable then moves independently.
foreach: aforeachloop does not move the internal pointer, so it neither disturbs nor depends onprev().- Key order: "previous" means previous in the array's insertion order, not numerically lower keys.
Common mistakes
- Testing with a loose condition.
if (prev($a))treats0,"",nullandfalseelements as "no element". Use=== false, and consultkey()when the array can holdfalse. - Forgetting the starting position. A fresh array's pointer is on the first element, so calling
prev()right away returnsfalse. Callend()first to go backwards through the whole array. - Passing a non-variable. Because of the by-reference parameter,
prev([1, 2, 3])is not valid. Store the array in a variable. - Relying on the pointer in shared code. Another function that calls
next(),reset()orend()on the same array variable changes the position you rely on. Preferforeachor explicit indexes when you do not need manual stepping. - Using it on objects. This has been deprecated since PHP 8.1.
Related functions
current()returns the element at the pointer without moving it.key()returns the key at the pointer, ornullwhen it is invalid.next()advances the pointer one position.reset()moves the pointer to the first element.end()moves the pointer to the last element.
Recap
prev() moves an array's internal pointer back by one and returns the new current value, or false when there is no earlier element. Pass a variable, position the pointer first with end() if you want to go backwards, compare results strictly, and use key() to separate a real false element from the start of the array.
Tested examples
$transport = ['foot', 'bike', 'car', 'plane'];
next($transport);
next($transport);
echo current($transport), "\n";
echo prev($transport), "\n";
echo prev($transport), "\n";
var_dump(prev($transport));
car bike foot bool(false)
$flags = [true, false, true];
end($flags);
$value = prev($flags);
echo json_encode($value), "\n";
echo json_encode(key($flags)), "\n";
prev($flags);
var_dump(prev($flags));
echo json_encode(key($flags)), "\n";
false 1 bool(false) null
$items = ['a' => 10, 'b' => 0, 'c' => 30];
end($items);
$result = [];
while (key($items) !== null) {
$result[key($items)] = current($items);
prev($items);
}
echo json_encode($result), "\n";
{"c":30,"b":0,"a":10}