PHP | next() Function

The next() function is an inbuilt function in PHP and does the following operations:

  • It is used to return the value of the next element in an array which the internal pointer is currently pointing to. We can know the current element by current function.
  • The next() function increments the internal pointer after returning the value.
  • In PHP, all arrays have an internal pointer. This internal pointer points to some element in that array which is called as the current element of the array.
  • Usually, the next element at the beginning is the second inserted element in the array.

Syntax:

next($array)

Parameter: It accepts only one parameter $array. This parameter is mandatory. It is the array in which we need to find the next element.



Return Value: The function returns the value of the next element in an array of which the internal pointer is currently pointing to. It returns FALSE if there is no element at next. Initially the next() function returns the second inserted element.

Examples:

Input : array = [1, 2, 3, 4] 
Output : 2 

Input : array = [1, 2, 3, 4], next() function executed two times      
Output : 2
         3 

Below programs illustrate the next() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to demonstrate the
// first position of next() function 
  
$array = array("geeks", "Raj", "striver",
                         "coding", "RAj");
  
echo next($array);
  
?>

chevron_right


Output:

Raj

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to demonstrate the 
// working of next() function 
  
$array = array("geeks", "Raj", "striver", "coding", "RAj");
  
// prints the initial current element (geeks)  
echo current($array), "\n"
  
// prints the initial next element (Raj)
// moves the pointer forward
echo next($array), "\n";  
  
// prints the  current element (Raj)  
echo current($array), "\n";  
  
// prints the  next element (striver)
// moves the pointer forward
echo next($array), "\n";  
  
// prints the  current element (striver)  
echo current($array), "\n";  
  
// prints the  next element (coding)
// moves the pointer forward
echo next($array), "\n";  
  
// prints the  current element (coding)  
echo current($array), "\n"
?>

chevron_right


Output:

geeks
Raj
Raj
striver
striver
coding
coding

Reference:
http://php.net/manual/en/function.next.php



My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.