PHP | end() Function

The end() function is an inbuilt function in PHP and is used to find the last element of the given array. The end() function changes the internal pointer of an array to point to the last element and returns the value of the last element.

Syntax:

end($array)

Parameters: This function accepts a single parameter $array. It is the array of which the last element we want to find.



Return Value: It returns the value of the last element of the array on success but FALSE on failure i.e, when the array is empty.

Examples:

Input: array('Ram', 'Shita', 'Geeta')
Output: Geeta
Explanation: Here input array contain many 
elements but output is Geeta i.e, last element 
of the array as the end() function returns the
last element of an array.

Below programs illustrate the end() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// input array
$arr = array('Ram', 'Shita', 'Geeta');
  
// end function print the last
// element of the array.
echo end($arr); 
  
?>

chevron_right


Output:

Geeta

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// input array
$arr = array('1', '3', 'P');
  
// end function print the last 
// element of the array.
echo end($arr)."\n";
  
// end() updates the internal pointer
// to point to last element as the 
// current() function will now also 
// return last element
echo current($arr);
  
?>

chevron_right


Output:

P
P

Reference:
http://php.net/manual/en/function.end.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.