PHP next() function

Example

Output the value of the current element and the next element in the array:

<?php
$people = array("Bill", "Steve", "Mark", "David");
echo current($people) . "<br>";
echo next($people);
?>

Run Examples

Definition and usage

The next() function points the internal pointer to the next element in the array and outputs.

Related methods:

  • prev() - Moves the internal pointer to the previous element in the array and outputs
  • current() - Returns the value of the current element in the array
  • end() - Moves the internal pointer to the last element in the array and outputs
  • reset() - Moves the internal pointer to the first element in the array and outputs
  • each() - Returns the key name and key value of the current element and moves the internal pointer forward

Syntax

next(array)
Parameter Description
array Required. Specifies the array to be used.

Description

The behavior of next() and current() is similar, with one difference: it moves the internal pointer one position forward before returning the value. This means it returns the value of the next array unit and moves the array pointer forward. If moving the pointer results in exceeding the end of the array unit, next() returns FALSE.

Note:If the array contains empty units, or the value of the unit is 0, the function will also return FALSE when encountering these units. To correctly traverse an array that may contain empty units or units with a value of 0, please refer to the each() function.

Technical details

Return value: If successful, it returns the value of the next element in the array, or FALSE if there are no more array elements.
PHP Version: 4+

More Examples

Example 1

Demonstrate all related methods:

<?php
$people = array("Bill", "Steve", "Mark", "David");
echo current($people) . "<br>"; // The current element is Bill
echo next($people) . "<br>"; // Bill's next element is Steve
echo current($people) . "<br>"; // The current element is now Steve
echo prev($people) . "<br>"; // The previous element of Steve is Bill
echo end($people) . "<br>"; // The last element is David
echo prev($people) . "<br>"; // The element before David is Mark
echo current($people) . "<br>"; // The current element is Mark
echo reset($people) . "<br>"; // Moves the internal pointer to the first element of the array, that is, Bill
echo next($people) . "<br>"; // Bill's next element is Steve
print_r (each($people)); // Returns the key name and value of the current element (currently Steve) and moves the internal pointer forward
?>

Run Examples