Home>Article>Backend Development> How to use php prev function
php prev function is used to point the internal pointer to the previous element in the array and output it. Its syntax is prev(array), and the parameter array is required.
#How to use php prev function?
Definition and usage
#prev() function points the internal pointer to the previous element in the array and outputs it.
Related methods:
next() - Point the internal pointer to the next element in the array and output
current() - Return The value of the current element in the array
end() - Sets the internal pointer to the last element in the array and outputs
reset() - Sets the internal pointer to the first element in the array element, and output
each() - Returns the key name and key value of the current element, and moves the internal pointer forward
Syntax
prev(array)
Parameter
array required.
Description
prev() behaves similarly to next(), except that it rewinds the internal pointer one position instead of moving it forward.
Note:
If the array contains empty cells, or the value of the cell is 0, this function will also return FALSE when encountering these cells. To correctly iterate over an array that may contain empty cells or a cell value of 0, see the each() function.
Return value: If successful, return the value of the previous element in the array, if there are no more array elements, return FALSE.
PHP Version: 4
Example 1
Demonstrates all relevant methods:
"; // 当前元素是 Bill echo next($people) . "
"; // Bill 的下一个元素是 Steve echo current($people) . "
"; // 现在当前元素是 Steve echo prev($people) . "
"; // Steve 的上一个元素是 Bill echo end($people) . "
"; // 最后一个元素是 David echo prev($people) . "
"; // David 之前的元素是 Mark echo current($people) . "
"; // 目前的当前元素是 Mark echo reset($people) . "
"; // 把内部指针移动到数组的首个元素,即 Bill echo next($people) . "
"; // Bill 的下一个元素是 Steve print_r (each($people)); // 返回当前元素的键名和键值(目前是 Steve),并向前移动内部指针 ?>
Output:
Bill Steve Steve Bill David Mark Mark Bill Steve Array ( [1] => Steve [value] => Steve [0] => 1 [key] => 1 )
Example 2
Output the values of the current element, next element and previous element in the array:
"; echo next($people) . "
"; echo prev($people); ?>
Output:
Bill Steve Bill
The above is the detailed content of How to use php prev function. For more information, please follow other related articles on the PHP Chinese website!