Tuesday, November 19, 2024
Google search engine
HomeLanguagesPHP end() Function

PHP end() Function

In this article, we will see how to get the last element in an array using the end() function in PHP, along with understanding their implementation through the examples.

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, otherwise returns FALSE on failure when the array is empty.

Consider the following example.

Input: array('Ram', 'Shita', 'Geeta')
Output: Geeta
Explanation: Here, the 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.

Example 1: The below example illustrate the end() function in PHP.

PHP




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


Output:

Geeta

Example 2: This example illustrates retrieving the last element from an array.

PHP




<?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);
?>


Output:

P
P

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

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.

RELATED ARTICLES

Most Popular

Recent Comments