PHP next() function with example

PHP next() function with example

PHP next() function: Here, we will find out about the following() function with example in PHP.

PHP next() function

next() function right off the bat moves the present pointer to the following component and returns the component.

Syntax:

next(array); 

Note: As we have talked about in the last post (current() function in PHP) that a cluster has a pointer that focuses to the primary component naturally, next() function can be utilized to move the pointer to next component and afterwards print it.

Example:

    Input:
    $arr1 = array(10, 20, 30, 40, 50);
    Function call:
    next($arr1);
    Output:
    20

    Input:
    $arr2 = array("name" => "Kishan", "age" => 21, "Gender" => "Male");
    Function call:
    next($arr2);
    Output:
    21

PHP Code:

<?php
    $arr1 = array(10, 20, 30, 40, 50);
    $arr2 = array("name" => "Kishan", "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

10
20
30
Kishan
21
Male

Recent Posts

error: Alert: Content is protected!!