PHP prev Function
last modified March 13, 2025
The PHP prev
function moves an array's internal pointer to the
previous element and returns its value. It's part of PHP's array navigation.
Basic Definition
The prev
function moves the internal array pointer backward.
It returns the value of the previous element or false if no more elements.
Syntax: prev(array &$array): mixed
. The function affects the
array's internal pointer. It's the opposite of next
.
Basic prev Example
This demonstrates moving backward through an array using prev
.
<?php $fruits = ['Apple', 'Banana', 'Cherry']; // Move to end first end($fruits); echo prev($fruits) . "\n"; echo prev($fruits) . "\n"; var_dump(prev($fruits));
We first move to the end with end
, then use prev
twice. The third call returns false as we've reached the array's start.
Combining with next
This shows how prev
and next
work together.
<?php $colors = ['Red', 'Green', 'Blue']; // Start at first element reset($colors); echo next($colors) . "\n"; echo prev($colors) . "\n"; echo next($colors) . "\n";
We reset the pointer, move forward with next
, then back with
prev
, then forward again. The pointer moves as expected.
In a Loop
Using prev
in a loop to traverse an array backward.
<?php $numbers = [10, 20, 30, 40]; // Start at end end($numbers); while ($current = prev($numbers)) { echo $current . "\n"; }
This outputs 40, 30, 20, 10. The loop stops when prev
returns
false. Note it doesn't process the first element in this approach.
With Associative Arrays
prev
works with associative arrays just like indexed ones.
<?php $user = [ 'name' => 'Alice', 'age' => 25, 'email' => 'alice@example.com' ]; end($user); do { $key = key($user); $value = current($user); echo "$key: $value\n"; } while (prev($user));
This outputs the array in reverse order. The do-while
ensures
we process the current element before moving the pointer.
Edge Case: Empty Array
How prev
behaves with an empty array.
<?php $empty = []; end($empty); var_dump(prev($empty));
With no elements, prev
immediately returns false. This matches
its behavior when reaching the start of a non-empty array.
Best Practices
- Reset First: Use
reset
orend
to ensure pointer position. - Check Return: Always verify the return value isn't false.
- Combine with key(): Use
key
to get the current key. - Avoid Modification: Don't modify arrays while iterating.
Source
This tutorial covered the PHP prev
function with practical
examples showing its usage for array navigation scenarios.
Author
List all PHP Array Functions.