prev: to move the cursor to previous element of an array

WE can rewind of move backward the internal pointer by one step by using prev()
prev(array) ;
We will move to the last element of the array by using end() and then apply prev(). Example
$my_array=array("First One", "Second One", "Third One", "Fourth One", "Fifth One");
end($my_array); // Move to last element 
echo prev($my_array); // Output : Fourth One
It returns False when the pointer or cursor is in first element of the array or not pointing to any element.
$my_array=array("First One", "Second One", "Third One", "Fourth One", "Fifth One");
if(prev($my_array)){
echo " Current value : ".current($my_array);
}else{
echo "The pointer moved beyond last element or the array is empty ";
}
In above case the cursor is in first element when the array is created so the prev() function will return FALSE.
$my_array=array("First One", "Second One", "Third One", "Fourth One", "Fifth One");

echo current($my_array); // Output : First One 
echo next($my_array); // Output : Second One 
echo next($my_array); // Output : Third One 
echo prev($my_array); // Output : Second One 
echo end($my_array); // Output : Fifth One 
echo reset($my_array); // Output : First One

Example: Traversing an Array in Reverse

$arr = ['apple', 'banana', 'cherry', 'date'];
end($arr);  // Move internal pointer to the end
echo prev($arr);  // Output: cherry

Example: Using array_prev() at the Beginning of an Array

$arr = ['apple', 'banana', 'cherry'];
echo prev($arr);  // Output: false (since the pointer is at the beginning)

Array REFERENCE
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer