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:
<?php // input array $arr = array ( 'Ram' , 'Shita' , 'Geeta' ); // end function print the last // element of the array. echo end ( $arr ); ?> |
Output:
Geeta
Program 2:
<?php // input array $arr = array ( '1' , '3' , 'P' ); // end function print the last // element of the array. echo end ( $arr ). "
" ; // end() updates the internal pointer // to point to last element as the // current() function will now also // return last element echo current( $arr ); ?> |
Output:
P P
Reference:
http://php.net/manual/en/function.end.php
leave a comment
0 Comments