Arrays7 min readExamples run on PHP 8.5.10

prev()

Learn how prev() moves an array's internal pointer back one position, what it returns at the start, and how to avoid false-value pitfalls.

Signature

prev(array|object &$array): mixed

Purpose

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.

Example
<?php
$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));
Output
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.

Example
<?php
$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";
Output
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.

Example
<?php
$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";
Output
{"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() returns false and the pointer becomes invalid. Further next() or prev() calls will not bring it back; call reset() or end() to reposition it.
  • Empty array: returns false immediately.
  • Copies: assigning an array to another variable copies the array value together with its pointer position, and each variable then moves independently.
  • foreach: a foreach loop does not move the internal pointer, so it neither disturbs nor depends on prev().
  • Key order: "previous" means previous in the array's insertion order, not numerically lower keys.

Common mistakes

  1. Testing with a loose condition. if (prev($a)) treats 0, "", null and false elements as "no element". Use === false, and consult key() when the array can hold false.
  2. Forgetting the starting position. A fresh array's pointer is on the first element, so calling prev() right away returns false. Call end() first to go backwards through the whole array.
  3. Passing a non-variable. Because of the by-reference parameter, prev([1, 2, 3]) is not valid. Store the array in a variable.
  4. Relying on the pointer in shared code. Another function that calls next(), reset() or end() on the same array variable changes the position you rely on. Prefer foreach or explicit indexes when you do not need manual stepping.
  5. Using it on objects. This has been deprecated since PHP 8.1.
  • current() returns the element at the pointer without moving it.
  • key() returns the key at the pointer, or null when 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

Stepping backwards through an array
<?php
$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));
Output
car
bike
foot
bool(false)
Telling a false element from the start of the array
<?php
$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";
Output
false
1
bool(false)
null
Reverse walk using key() as the stop condition
<?php
$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";
Output
{"c":30,"b":0,"a":10}