PHP next() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, next() moves the internal array pointer one step forward and returns the value of the new current element.

Every array has a hidden pointer. current() reads the element the pointer is on, and next() moves it to the following element. This is a manual way to walk through an array, step by step.

Syntax

Call next() with the values you need. Start with this line.

PHP
$value = next($colors);
CallWhat it does
next($colors)Moves forward by one element and returns its value.
current($colors)Reads the value at the pointer without moving it.
next($finished)Returns false when there are no more elements.

next() changes the pointer first and then returns the value. It does not return the element you were on. It changes only the pointer, never the array contents.

Library declarations
PHP
next(array|object &$array): mixed

Parameters

ParameterDescription
arrayRequired. The array whose internal pointer moves forward. It is passed by reference.

Return Value

Return ValueDescription
mixedThe value of the next element, or false if there are no more elements.

PHP Examples

Example 1 — Step Through an Array

This example starts at the first element and moves forward twice.

PHP
<?php
$colors = ["red", "green", "blue"];

echo current($colors), PHP_EOL;
echo next($colors), PHP_EOL;
echo next($colors), PHP_EOL;

How It Works

  1. The pointer starts on the first element, so current() returns "red".
  2. The first next() moves to the second element and returns "green".
  3. The second next() moves to the third element and returns "blue".
  4. Each echo prints the value that the pointer is now on.

Example 2 — next() Returns false at the End

When there is no element after the current one, next() returns false.

PHP
<?php
$letters = ["a", "b"];

var_dump(next($letters));
var_dump(next($letters));

How It Works

  1. The array has two elements and the pointer starts on "a".
  2. The first next() moves to "b" and returns it.
  3. The second next() tries to move past the last element and returns false.
  4. var_dump() shows the string first and then the boolean.

Example 3 — Add Up Values with a while Loop

next() can drive a loop. The loop stops when next() returns false.

PHP
<?php
$numbers = [5, 10, 15];
$total = 0;

$value = current($numbers);
while ($value !== false) {
    $total += $value;
    $value = next($numbers);
}

echo $total, PHP_EOL;

How It Works

  1. current() gives the first value, 5, and the total starts at 0.
  2. Inside the loop the value is added to $total.
  3. next() moves the pointer and returns the next value. At the end it returns false and the loop stops.
  4. The total is 5 + 10 + 15, which is 30.

Frequently Asked Questions

It moves the internal pointer of an array forward by one element and returns the value of that new element, or false if there is none.
current() only reads the value at the pointer. next() moves the pointer first and then returns the value.
Use prev() to go back one step, reset() to go to the first element, or end() to go to the last.
The pointer has moved beyond the last element. It can also be false if an element in your array has the value false.
Usually no. foreach is shorter and safer. Use next() only when you need to control the pointer yourself.

Next: pos()

pos() returns the value of the element the internal array pointer is on. It is an alias of.

pos() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful