PHP prev() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, prev() moves the internal array pointer one step backward and returns the value of the new current element.

prev() is the reverse of next(). Together with end() it lets you read an array from the last element to the first, one element at a time.

Syntax

Call prev() with the values you need. Start with this line.

PHP
$value = prev($letters);
CallWhat it does
prev($letters)Moves back by one element and returns its value.
end($letters)Moves the pointer to the last element so prev() can walk backward from there.
prev($start)Returns false when the pointer is already on the first element.

prev() moves the pointer first and then returns the value. It changes only the pointer and never the array contents. If the pointer is on the first element, prev() moves it off the array and returns false.

Library declarations
PHP
prev(array|object &$array): mixed

Parameters

ParameterDescription
arrayRequired. The array whose internal pointer moves backward. It is passed by reference.

Return Value

Return ValueDescription
mixedThe value of the previous element, or false if there is no previous element.

PHP Examples

Example 1 — Walk Backward from the End

end() puts the pointer on the last element, and prev() steps backward from there.

PHP
<?php
$numbers = [1, 2, 3];

end($numbers);
echo prev($numbers), PHP_EOL;
echo prev($numbers), PHP_EOL;

How It Works

  1. end() moves the pointer to the last element, 3.
  2. The first prev() moves back to 2 and returns it.
  3. The second prev() moves back to 1 and returns it.
  4. Each echo prints the value that the pointer reached.

Example 2 — prev() at the Start of the Array

There is nothing before the first element, so prev() returns false and the pointer becomes invalid.

PHP
<?php
$numbers = [1, 2];

var_dump(prev($numbers));
var_dump(key($numbers));

How It Works

  1. The pointer starts on the first element, which has no element before it.
  2. prev() returns false because it cannot go further back.
  3. The pointer is now outside the array, so key() has no key to return.
  4. var_dump() shows NULL for the key.

Example 3 — Print an Array in Reverse

This loop uses end() and prev() to print the values from last to first.

PHP
<?php
$letters = ["a", "b", "c"];

$value = end($letters);
while ($value !== false) {
    echo $value;
    $value = prev($letters);
}

echo PHP_EOL;

How It Works

  1. end() returns the last value, "c", and the loop starts with it.
  2. Each loop prints the current value.
  3. prev() moves back one element and returns the value, "b" and then "a".
  4. After "a" prev() returns false, the loop stops, and the output is cba.

Frequently Asked Questions

It moves the internal pointer of an array back by one element and returns the value at the new position, or false if there is no such element.
next() moves the pointer forward and prev() moves it backward. Both return the value of the element they land on.
A simple way is foreach (array_reverse($array) as $value). With the pointer functions you can use end() first and then prev() in a while loop.
The pointer is already on the first element, or the array is empty. A stored false value can also cause it.
It is no longer on any element. Use reset() or end() to put it on the first or last element again.

Next: range()

range() creates an array that contains a sequence of numbers or letters, from a start valu.

range() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful