PHP | reset() Function

The reset() function is an inbuilt function in PHP.

  • This function is used to move any array’s internal pointer to the first element of that array.
  • While working with arrays, it may happen that we modify the internal pointer of an array using different functions like prev() function, current() function, key() function etc.
  • The reset() function resets the internal pointer to point to the first element of the array.

Syntax:

reset($array)

Parameters: This function accepts a single parameter $array. It is the array for which we want to reset the internal pointer to point to the first element again.



Return Value: It returns the first element of the array on success, or FALSE if the array is empty i.e, the array does not contain any element.

Below programs illustrate the reset() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// input array
$arr = array('Ram', 'Rahim', 'Geeta', 'Shita');
  
// here reset() function Moves the internal pointer to the
// first element of the array, which is Ram and also returns
// the first element
$res = reset($arr);
print "$res";
  
?>

chevron_right


Output:

Ram

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Input array
$arr = array('Delhi', 'Kolkata', 'London');
  
// getting current element using current()
// function
print current($arr)."\n";
  
// move internal pointer to next element
next($arr);
  
print current($arr)."\n";
  
// now reset() is called so that the internal pointer 
// moves to the first element again i.e, Delhi.
reset($arr);
print current($arr);
  
?>

chevron_right


Output:

Delhi
Kolkata
Delhi

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.