How to use php prev function

藏色散人
Release: 2023-02-23 06:16:02
Original
2954 people have browsed it

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

#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)
Copy after login

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:

<?php
$people = array("Bill", "Steve", "Mark", "David");
echo current($people) . "<br>"; // 当前元素是 Bill
echo next($people) . "<br>"; // Bill 的下一个元素是 Steve
echo current($people) . "<br>"; // 现在当前元素是 Steve
echo prev($people) . "<br>"; // Steve 的上一个元素是 Bill
echo end($people) . "<br>"; // 最后一个元素是 David
echo prev($people) . "<br>"; // David 之前的元素是 Mark
echo current($people) . "<br>"; // 目前的当前元素是 Mark
echo reset($people) . "<br>"; // 把内部指针移动到数组的首个元素,即 Bill
echo next($people) . "<br>"; // Bill 的下一个元素是 Steve
print_r (each($people)); // 返回当前元素的键名和键值(目前是 Steve),并向前移动内部指针
?>
Copy after login

Output:

Bill
Steve
Steve
Bill
David
Mark
Mark
Bill
Steve
Array ( [1] => Steve [value] => Steve [0] => 1 [key] => 1 )
Copy after login

Example 2

Output the values ​​of the current element, next element and previous element in the array:

<?php
$people = array("Bill", "Steve", "Mark", "David");
echo current($people) . "<br>";
echo next($people) . "<br>";
echo prev($people);
?>
Copy after login

Output:

Bill
Steve
Bill
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template