next: to move the cursor to next element of an array

next(): advance the internal array pointer

next() advances the internal pointer and returns the new current value. It can return false both at the end and for a false-valued element, so key() is useful when position must be unambiguous.

$items = ['red', 'green', 'blue'];
reset($items);
echo next($items);      // green
echo key($items);       // 1

foreach does not modify the internal array pointer, so pointer demonstrations should use current()/next()/key() directly.

Array next() returns the next element and shifts the internal pointer by one.
next($array);
Let us create one array and display the second element. In a new array the internal pointer will remain at the first element, so the next() function will shift the pointer to second element and returns the second element.
Example
$my_array=array("First One", "Second One", "Third One", "Fourth One", "Fifth One");
echo next($my_array); // Second One
It returns False when the pointer or cursor is moved beyond the last element of the array or not pointing to any element.
$my_array=array("First One", "Second One", "Third One", "Fourth One", "Fifth One");
while (key($my_array) !== null) {
$key = key($my_array);
$val = current($my_array);
next($my_array); 
echo "$key -> $val <br>"; 
}
if(next($my_array)){
echo " Current value : ".current($my_array);
}else{
echo "The pointer moved beyond last element or the array is empty ";
}
In above case the cursor has moved beyond the last element of the array ( after displaying all elements inside loop ) so the next() function will return FALSE.
$my_array=array("First One", "Second One", "Third One", "Fourth One", "Fifth One");

echo current($my_array); // Output : First One 
echo next($my_array); // Output : Second One 
echo next($my_array); // Output : Third One 
echo prev($my_array); // Output : Second One 
echo end($my_array); // Output : Fifth One 
echo reset($my_array); // Output : First One
Array REFERENCE


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer