PHP next() Function

What is PHP next() Function?

PHP next() function moves the internal pointer to the next element and returns its value. Every array has an internal pointer that always points to one array element at any given time. That element is called the current element of that array which is initialized to the first element of the array.

Syntax:

next(array)

Parameters:

The Function has 1 parameter which is required-

array (Required): This is the array which the array operates on.

Return Values:

The function returns –

  • The value of the next element if success. Check example 1.
  • FALSE if there is no element in the array. Check example 2.

Examples:

Example 1:

<?php // #1
$array = array("CSS", "HTML", "PHP", "JavaScript");
echo "The value of the current element is: " . current($array);
echo "<br />";
echo "The value of the next element is: " . next($array);
echo "<br />";
echo "The value of the next element is: " . next($array);
echo "<br />";
echo "The value of the next element is: " . next($array);
?>

Output:

The value of the current element is: CSS
The value of the next element is: HTML
The value of the next element is: PHP
The value of the next element is: JavaScript

Example 2:

<?php
$array = array("CSS", "HTML", "PHP", "JavaScript");
echo "The value of the last element is: " . end($array);
echo "<br />";
echo "The value of the next element is: ";
var_dump(next($array));
?>

Output:

The value of the last element is: JavaScript
The value of the next element is: bool(false)

Notes on next() Function:

  • If you call the next() function at the beginning, you’ll get the second item of array as when an array is initialized, the internal pointer located at the first element of the array. Check example-
    <?php
    $array = array("CSS", "HTML", "PHP", "JavaScript");
    echo "The value of the last element is: " . end($array);
    echo "<br />";
    echo "The value of the next element is: " . next($array);
    var_dump(next($array));
    ?>
    

    Output:

    The value of the last element is: JavaScript
    The value of the next element is:

Caution:

  • When the internal pointer reach to the end and you call this function, it returns false which shows an empty value. An empty element also returns the value. To differentiate it, check its type. Check example-
    <?php
    $array = array("CSS", "HTML", "PHP", "");
    echo "The value of the last element is: "; var_dump(end($array));
    echo "<br />";
    echo "The value of the next element is: "; var_dump(next($array));
    ?>
    

    Output:

    The value of the last element is: string(0) "" 
    The value of the next element is: bool(false)

Difference between next() Function and current() Function:

The next() function moves the internal pointer to the next element and returns it’s value, on the other hand, the current() function returns the value of the current element.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP next() Function

next() is a built-in PHP array function that you can use to get the value of the next element of the current internal pointer.

Reference:

https://www.php.net/manual/en/function.next.php