PHP Function Reference

PHP next() Function



The PHP next() function returns the next array value and advances the internal array pointer by one. It behaves like current() function, with one difference. It advances the internal array pointer one place forward before returning the element value.

Syntax

next(array)

Parameters

array Required. Specify the input array.

Return Value

Returns the array value in the next place that is pointed to by the internal array pointer, or false if there are no more elements.

Note: This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Therefore, use === operator for testing the return value of this function.

Exceptions

NA.

Example:

The example below shows the usage of next() function.

<?php
$Arr = array(10, 20, "Hello", "World");

echo current($Arr)."\n";   //prints 10
echo next($Arr)."\n";      //prints 20
echo next($Arr)."\n";      //prints Hello
echo prev($Arr)."\n";      //prints 20
echo end($Arr)."\n";       //prints World

echo "\n";
$Arr1 = array();
var_dump(next($Arr1));     //bool(false)

$Arr2 = array(array());
var_dump(next($Arr2));     //bool(false)
?>

The output of the above code will be:

10
20
Hello
20
World

bool(false)
bool(false)

Example:

Consider one more example where the next() function is used with an associative array.

<?php
$Arr = array(10=>"Red",
             20=>"Green",
             30=>"Blue",
             40=>"Black",
             50=>"White");

echo current($Arr)."\n";   //prints Red
echo next($Arr)."\n";      //prints Green
echo next($Arr)."\n";      //prints Blue
echo prev($Arr)."\n";      //prints Green
echo end($Arr)."\n";       //prints White
?>

The output of the above code will be:

Red
Green
Blue
Green
White

❮ PHP Array Reference