next()函数是PHP中的内置函数,它执行以下操作:
它用于返回内部指针当前指向的数组中下一个元素的值。我们可以通过当前函数知道当前元素。
返回值后,next()函数使内部指针递增。
在PHP中,所有数组都有一个内部指针。此内部指针指向该数组中的某个元素,该元素称为数组的当前元素。
通常,开头的下一个元素是数组中插入的第二个元素。
用法:
next($array)
参数:它仅接受一个参数$array。此参数是必需的。它是我们需要在其中找到下一个元素的数组。
返回值:该函数返回内部指针当前指向的数组中下一个元素的值。如果下一个没有元素,则返回FALSE。最初,next()函数返回第二个插入的元素。
例子:
Input : array = [1, 2, 3, 4]
Output : 2
Input : array = [1, 2, 3, 4], next() function executed two times
Output : 2
3
以下示例程序旨在说明PHP中的next()函数:
程序1:
// PHP Program to demonstrate the
// first position of next() function
$array = array("geeks", "Raj", "striver",
"coding", "RAj");
echo next($array);
?>
输出:
Raj
程序2:
// PHP Program to demonstrate the
// working of next() function
$array = array("geeks", "Raj", "striver", "coding", "RAj");
// prints the initial current element (geeks)
echo current($array), "\n";
// prints the initial next element (Raj)
// moves the pointer forward
echo next($array), "\n";
// prints the current element (Raj)
echo current($array), "\n";
// prints the next element (striver)
// moves the pointer forward
echo next($array), "\n";
// prints the current element (striver)
echo current($array), "\n";
// prints the next element (coding)
// moves the pointer forward
echo next($array), "\n";
// prints the current element (coding)
echo current($array), "\n";
?>
输出:
geeks
Raj
Raj
striver
striver
coding
coding