Home >>PHP Array Functions >PHP prev() Function

PHP prev() Function

PHP prev() Function

PHP prev() function is used to return the immediate previous element from the given input array of the element which is currently pointed by the internal pointer. It accepts only a single parameter $array that is the given input array. It returns the value of that element in the array which is just before the element internal pointer is currently pointing to.

Syntax:

prev($array);

Parameter Values

Parameter Description
array This is a required parameter. It defines the array to use.

Here is an example of prev() function in PHP:

<html>
<body>
<?php
$x = array("A","B","C","D","E","F","G");
echo current($x)."<br>";
echo next($x)."<br>";
echo prev($x);
?>
</body>
</html>
Output:
A
B
A

Example 2:

<html>
<body>
<?php
$x = array("A","B","C","D","E","F","G");
echo current($x)."<br>";
echo next($x)."<br>";
echo prev($x)."<br>";
echo current($x)."<br>";
echo next($x)."<br>"; 
echo current($x)."<br>";
echo prev($x)."<br>";
echo end($x)."<br>"; 
echo prev($x)."<br>";
echo current($x)."<br>";
echo reset($x)."<br>";
echo next($x);
?>
</body>
</html>
Output:
A
B
A
A
B
B
A
G
F
F
A
B

No Sidebar ads