Home » PHP

PHP next() function with example

PHP next() function: Here, we are going to learn about the next() function with example in PHP.
Submitted by IncludeHelp, on February 26, 2019

PHP next() function

next() function firstly moves the current pointer to the next element and returns the element.

Syntax:

    next(array);

Note: As we have discussed in the last post (current() function in PHP) that an array has a pointer that points to the first element by default, next() function can be used to move the pointer to next element and then print it.

Examples:

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

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

PHP code:

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.