Home »
PHP
PHP Array next() Function (With Examples)
In this tutorial, we will learn about the PHP next() function with its usage, syntax, parameters, return value, and examples.
By IncludeHelp Last updated : December 31, 2023
PHP next() function
The next() function firstly moves the current pointer to the next element and returns the element.
Syntax
The syntax of the next() function:
next(arr);
Parameters
The parameters of the next() function:
Return Value
The return type of this method is mixed, it returns the array value in the next place that's pointed to by the internal array pointer, or false if there are no more elements. [Source]
Note
As we have discussed in the previous tutorial (current() function) that an array has a pointer that points to the first element by default, next() function can be used to move the pointer to next element and then print it.
Sample Input/Output
Input:
$arr1 = array(10, 20, 30, 40, 50);
Function call:
next($arr1);
Output:
20
Input:
$arr2 = array("name" => "Amit", "age" => 21, "Gender" => "Male");
Function call:
next($arr2);
Output:
21
PHP next() Function Example
<?php
$arr1 = array(10, 20, 30, 40, 50);
$arr2 = array("name" => "Amit", "age" => 21, "Gender" => "Male");
echo current($arr1) . "\n"; //print first element
echo next($arr1) ."\n"; //move to next element and print
echo next($arr1) ."\n"; //move to next element and print
echo current($arr2) . "\n"; //print first element
echo next($arr2) ."\n"; //move to next element and print
echo next($arr2) ."\n"; //move to next element and print
?>
Output
The output of the above example is:
10
20
30
Amit
21
Male
To understand the above example, you should have the basic knowledge of the following PHP topics: