prev

Rewinds an array's internal pointer one position and returns the value found there.

Signature
prev(array|object &$array): mixed
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$arrayarray|objectThe array whose internal pointer is rewound. Passed by reference. Passing an object is deprecated as of PHP 8.1.

Return value

mixedThe value at the new pointer position, or false if the pointer has moved before the first element.

Errors

Nothing is thrown. Passing an object emits an E_DEPRECATED notice as of PHP 8.1.0.

Changelog

  • PHP 8.1Calling this function on objects is deprecated. Convert the object with get_mangled_object_vars() first, or use a class implementing Iterator such as ArrayIterator.
  • PHP 7.4Instances of SPL classes are treated like empty objects with no properties instead of calling the Iterator method of the same name.

prev is the mirror of next: it moves the array's internal pointer back one slot and returns the value there. Step back past the first element and it returns false, leaving the pointer in an invalid position from which next will not recover — you have to call reset or end to put it somewhere valid again.

When to reach for it

The common use is backtracking. A hand-written parser peeks at the following token with next, decides it does not want it, and calls prev to restore position. It also pairs with end when you want to walk an array from the last element toward the first without building a reversed copy.

What trips people up

Once the pointer falls off the front it is not simply at index -1; it is invalid, key returns null, and next keeps returning false instead of moving to element 0. That asymmetry catches people writing symmetric forward/backward loops.

As with next, a false return can mean either "before the start" or "this element is false". Test key($array) !== null instead. And because the parameter is by reference, the argument must be a variable, not a function call result.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Restore position in a token stream after peeking ahead with next().
  • Walk an array from the last element to the first using end() then prev() in a loop.
  • Back up one row in a cursor-style reader when a lookahead decides the row belongs to the next group.

Frequently asked questions

What is the difference between prev and next?

next() moves the internal pointer forward one element, prev() moves it backward one element. Both return the value at the new position, or false if the move went out of bounds.

Why does next stop working after prev returns false?

Moving before the first element puts the pointer in an invalid state rather than at index 0, and next() cannot climb back out of it. Call reset($array) to restore a valid position.

How can I tell whether prev hit the start or returned a falsy value?

Check key($array) after the call: it returns null only when the pointer is out of bounds. A genuine element with the value false still has a key.

More examples

Walking an array backwards

The for loop starts at the last element and uses key() !== null as the stop condition, printing 2:third, 1:second, 0:first. reset() is needed afterwards because the loop left the pointer in the invalid before-the-start state.

Example · php
<?php
$rows = ['first', 'second', 'third'];

for (end($rows); key($rows) !== null; prev($rows)) {
    echo key($rows), ': ', current($rows), "\n";
}

reset($rows);
echo 'pointer back at: ', current($rows), "\n";

Peek and rewind

next() reads 'ready' as a lookahead; because it is not the token we were hoping for, prev() rewinds and current() reports 'IF' at key 0 again — the pointer is exactly where it started.

Example · php
<?php
$tokens = ['IF', 'ready', 'THEN', 'go'];
reset($tokens);

$lookahead = next($tokens);
if ($lookahead !== 'ELSE') {
    prev($tokens);
}

echo 'lookahead: ', $lookahead, "\n";
echo 'current:   ', current($tokens), "\n";
echo 'key:       ', key($tokens), "\n";

Discussion

  • Be the first to comment on this lesson.