PHP Function Reference

PHP end() Function



The PHP end() function advances the array's internal pointer to the last element, and returns its value.

Syntax

end(array)

Parameters

array Required. Specify the input array.

Return Value

Returns the value of the last element or false for empty array.

Note: This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Therefore, use === operator for testing the return value of this function.

Exceptions

NA.

Example:

The example below shows the usage of end() function.

<?php
$Arr = array(10, 20, "Hello", "World");

echo end($Arr)."\n";   //prints World
echo prev($Arr)."\n";  //prints Hello
echo prev($Arr)."\n";  //prints 20
echo end($Arr)."\n";   //prints World

echo "\n";
$Arr1 = array();
var_dump(end($Arr1));  //bool(false)

$Arr2 = array(array());
var_dump(end($Arr2));  //array(0) { }
?>

The output of the above code will be:

World
Hello
20
World

bool(false)
array(0) { }

Example:

Consider one more example where the end() function is used with an associative array.

<?php
$Arr = array(10=>"Red",
             20=>"Green",
             30=>"Blue",
             40=>"Black",
             50=>"White");

echo end($Arr)."\n";   //prints White
echo prev($Arr)."\n";  //prints Black
echo prev($Arr)."\n";  //prints Blue
echo end($Arr)."\n";   //prints White
?>

The output of the above code will be:

White
Black
Blue
White

❮ PHP Array Reference