The prev() function is an inbuilt function in PHP.
- It is used to return the immediate previous element from an array of the element which is currently pointed by the internal pointer.
- We have already discussed current() function in PHP.
- The current() function is used to return the value of the element which is currently pointed by the internal pointer whereas the prev() function decrements or make the internal pointer to point to the previous element of the currently pointed element.
Syntax:
prev($array)
Parameters: This function accepts a single parameter $array. It is the array of which we want to find the current element.
Return Value: It returns the value of the element in the array which is just before the element which the internal pointer is currently pointing to. If the array is empty then the prev() function returns FALSE.
Below programs illustrate the prev() function in PHP:
Program 1:
<?php
$arr = array ( "Ram" , "Shita" , "Geeta" , "Shyam" );
echo current( $arr ) . "\n" ;
echo next( $arr ). "\n" ;
echo prev( $arr );
?>
|
Output:
Ram
Shita
Ram
Program 2:
<?php
$arr = array ( 'a' , '2' , 'z' , '8' );
echo current( $arr ) . "\n" ;
echo next( $arr ). "\n" ;
echo next( $arr ). "\n" ;
echo prev( $arr );
?>
|
Output:
a
2
z
2
Reference:
http://php.net/manual/en/function.prev.php
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
20 Jun, 2023
Like Article
Save Article