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.
next(array|object &$array): mixed
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.
<?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));?>
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.