PHP prev() Function
The PHP prev() function rewinds the internal array pointer. It behaves just like next() function, except it rewinds the internal array pointer one place instead of advancing it.
Syntax
prev(array)
Parameters
array |
Required. Specify the input array. |
Return Value
Returns the array value in the previous place that is pointed to by the internal array pointer, or false if there are no more elements.
Note: This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Therefore, use === operator for testing the return value of this function.
Exceptions
NA.
Example:
The example below shows the usage of prev() function.
<?php $Arr = array(10, 20, "Hello", "World"); echo current($Arr)."\n"; //prints 10 echo next($Arr)."\n"; //prints 20 echo prev($Arr)."\n"; //prints 10 echo end($Arr)."\n"; //prints World echo prev($Arr)."\n"; //prints Hello echo "\n"; $Arr1 = array(); var_dump(prev($Arr1)); //bool(false) $Arr2 = array(array()); var_dump(prev($Arr2)); //bool(false) ?>
The output of the above code will be:
10 20 10 World Hello bool(false) bool(false)
Example:
Consider one more example where the prev() function is used with an associative array.
<?php $Arr = array(10=>"Red", 20=>"Green", 30=>"Blue", 40=>"Black", 50=>"White"); echo current($Arr)."\n"; //prints Red echo next($Arr)."\n"; //prints Green echo prev($Arr)."\n"; //prints Red echo end($Arr)."\n"; //prints White echo prev($Arr)."\n"; //prints Black ?>
The output of the above code will be:
Red Green Red White Black
❮ PHP Array Reference