What is the array next() method in PHP?

The next method is used to move the position of the internal pointer (IP) of the array one step forward and return the value.


In PHP, each array contains an internal pointer that points to the current element. It initially points to the first element of the array. We can move the pointer positions using methods like next, prev, etc.

Syntax


next(array|object &$array): mixed

Return value

The return is false if an array is empty or if the position pointed by the internal pointer is beyond the length of the array.

Code

<?php
$numbers = [1,2,3];
// 1
echo "Current Value is : ". current($numbers). "\n";
//2
echo "Next value is : ". next($numbers). "\n";
//3
echo "Next value is : ". next($numbers). "\n";
// beyond length of array so false
echo "Current Value is : ";
var_dump(next($numbers));
?>

Explanation

In the code above:

  • We created a numbers array.

  • We used the current method to print the value pointed by the array internal pointer.

  • We used the next method to move the pointer position one step forward and printed the value pointed by the pointer.

Free Resources