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 likenext,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];// 1echo "Current Value is : ". current($numbers). "\n";//2echo "Next value is : ". next($numbers). "\n";//3echo "Next value is : ". next($numbers). "\n";// beyond length of array so falseecho "Current Value is : ";var_dump(next($numbers));?>
Explanation
In the code above:
-
We created a
numbersarray. -
We used the
currentmethod to print the value pointed by the array internal pointer. -
We used the
nextmethod to move the pointer position one step forward and printed the value pointed by the pointer.