php8.5
Home/ Manual/ array / functions/ prev

prev

PHP function Edit on GitHub ✎

(PHP 4, PHP 5, PHP 7, PHP 8)

Rewind the internal array pointer

Description

prev(array|object $array): mixed

Rewind the internal array pointer.

prev() behaves just like next(), except it rewinds the internal array pointer one place instead of advancing it.

Parameters

array

The input array.

Return Values

Returns the array value in the previous place that's pointed to by the internal array pointer, or false if there are no more elements.

Falseproblem

Changelog

VersionDescription

Examples

Example use of prev() and friends

php
<?php
$transport = array('foot', 'bike', 'car', 'plane');
echo $mode = current($transport), PHP_EOL; // $mode = 'foot';
echo $mode = next($transport), PHP_EOL;    // $mode = 'bike';
echo $mode = next($transport), PHP_EOL;    // $mode = 'car';
echo $mode = prev($transport), PHP_EOL;    // $mode = 'bike';
echo $mode = end($transport), PHP_EOL;     // $mode = 'plane';
?>

Notes

Note

The beginning of an array is indistinguishable from a bool false element. To make the distinction, check that the key() of the prev() element is not null.

See Also

Source: reference/array/functions/prev.xml · from the official PHP manual (php/doc-en)