Current Location: Home> Latest Articles> Why does end() return false on an empty array?

Why does end() return false on an empty array?

M66 2025-05-14

In PHP programming, end() is a commonly used built-in function that points an internal pointer to the last element in the array and returns the value of that element. Generally, the end() function can successfully return the last element of the array for non-empty arrays. However, when the end() function is called, if the array is empty, the function returns false . So, why does this happen? Let's analyze it step by step.

The basic working principle of the end() function

The end() function is to return the last element of the array and point the internal pointer to this element. Its syntax is as follows:

 mixed end ( array &$array )
  • $array : The array to be operated on, the function will point its internal pointer to the last element of the array.

When the array is empty, the behavior of the end() function

When we pass an empty array to the end() function, the internal pointer points to the "end" of the array, but since the array has no elements, there is actually no value that can be returned. At this time, the end() function returns false , indicating that there is no element that can be provided.

Sample code:

 <?php
$array = [];
$result = end($array);

if ($result === false) {
    echo "Empty array call end() return false";
} else {
    echo "The last element is: " . $result;
}
?>

The output result is:

 Empty array call end() return false

The reasons behind this behavior

In PHP, the internal pointer of an array is a very important concept. Whenever we manipulate arrays, the pointer points to a different position in the array. For an empty array, the behavior of the internal pointer of the array is undefined or cannot point to any valid elements. Therefore, PHP will return false as a type of identifier indicating that in the case of an empty array, there is no last element that can be returned.

If you want to avoid this, you can check if the array is empty before calling the end() function. Usually, we can use the empty() function to do this check:

Sample code:

 <?php
$array = [];

if (empty($array)) {
    echo "The array is empty,Unable to call end() function";
} else {
    $result = end($array);
    echo "The last element is: " . $result;
}
?>

Suggestions for handling empty arrays

  1. Pre-check whether the array is empty: Use the empty() function to check whether the array is empty, making sure to avoid unnecessary errors before calling end() .

  2. Default: If you want to return a default value instead of false when the array is empty, you can do it with a ternary operator or conditional statement:

 <?php
$array = [];
$lastElement = !empty($array) ? end($array) : 'default value';
echo $lastElement;
?>

Summarize

In PHP, when the end() function is called, it returns false if the array is empty. This behavior indicates that there are no elements in the array and the last element cannot be returned. Understanding this helps avoid problems caused by empty arrays when programming, ensuring the stability and readability of the code.