Skip to content
PHP prev() Function: Move an Array’s Internal Pointer Backward

PHP prev() Function: Move an Array’s Internal Pointer Backward

The PHP prev() function moves an array’s internal pointer one step backward, returning the previous element’s value or FALSE when no more elements exist, and is demonstrated with a complete example showing how to iterate and retrieve values using prev, next, and current.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community

The mixed prev(array &$array) function moves the internal pointer of the given array one position backward.

Explanation: It behaves similarly to next() but decrements the pointer instead of advancing it.

Parameter: array – the array whose pointer will be affected.

Return value: The value of the array element now pointed to, or FALSE if the pointer is beyond the first element.

Example:

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

This example creates an array of transport modes, uses current() to get the first element, advances the pointer with next(), moves it back with prev(), and finally jumps to the last element with end().

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendprogrammingPHParrayFunctionprev
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.