Understanding PHP's array end() function
In PHP, arrays are one of the most commonly used data types. Arrays can store a collection of data in a single variable, which can be manipulated and iterated
In PHP, arrays are one of the most commonly used data types. Arrays can store a collection of data in a single variable, which can be manipulated and iterated over easily. One of the functions available for manipulating arrays is the end() function.
At its simplest, the end() function returns the last value of an array. Under the hood it also moves the array's internal pointer to the last element, which is the detail most people miss. In this article, we'll cover the end() function's syntax, how it works in practice, the way it interacts with the internal pointer, and the common pitfalls — empty arrays and associative arrays — that trip people up.
Syntax
The syntax of the end() function is straightforward:
end(array &$array): mixedWhere $array is the array whose last element we want to return. Two things are worth noting:
- The array is passed by reference (
&$array) becauseend()mutates the array's internal pointer. As a result you must pass a real variable, not an inline expression likeend([1, 2, 3])— that emits a notice in older PHP versions and a fatal error in PHP 8.1+. - The return type is
mixed: it returns the value of the last element, orfalseif the array is empty.
Usage
Let's take a closer look at how the end() function works in practice. Consider the following array:
A PHP Array
$fruits = array('apple', 'banana', 'cherry');To retrieve the last element of this array using end(), we would use the following code:
After executing this code, $last_fruit would contain the string 'cherry'.
Modifying the Array Pointer
Every PHP array keeps an internal pointer that tracks a "current" position. Calling end() moves that pointer to the last element. The other pointer functions move it too: reset() jumps back to the first element, next() advances one step, prev() steps back, and current() reads the element the pointer is sitting on without moving it.
Because end() leaves the pointer on the last element, calling current() right after it returns that same last value:
<?php
$fruits = ['apple', 'banana', 'cherry'];
end($fruits);
echo current($fruits);This code will output the following:
cherryIf you need to iterate over an array in reverse order, do not try to do it by repeatedly calling end() — the pointer never moves past the last element, so a loop driven by end() would never terminate. Instead, use array_reverse() (or a standard indexed for loop counting down from count($array) - 1):
<?php
$fruits = ['apple', 'banana', 'cherry'];
foreach (array_reverse($fruits) as $fruit) {
echo $fruit . "<br>";
}This code will output the following:
cherry
banana
appleCommon Pitfalls
There are a few common pitfalls to be aware of when using the end() function:
Empty Arrays
If the array passed to end() is empty, the function returns false. This is ambiguous: false is also a legitimate value a non-empty array could hold, so a bare check can give you the wrong answer.
To be safe, check whether the array is empty first (with empty() or count()), or use the strict !== false comparison:
Associative Arrays
The end() function always returns the value of the last element, regardless of whether the array is indexed or associative:
If you need to retrieve the last key of an associative array, use the array_key_last() function instead:
end() vs array_pop()
A common point of confusion is the difference between end() and array_pop(). Both give you the last element, but:
end()reads the last value and leaves the array untouched (it only moves the internal pointer).array_pop()removes the last element and returns it, shrinking the array by one.
Use end() when you just want to peek at the last value; use array_pop() when you actually want to take it off the array.
Related functions
The end() function is part of PHP's internal-pointer family. These chapters cover the rest:
reset()— move the pointer to the first element and return it.current()— read the element at the current pointer position.next()— advance the pointer and return the next element.prev()— rewind the pointer and return the previous element.array_reverse()— get a reversed copy of the array for clean reverse iteration.array_pop()— remove and return the last element.
Conclusion
In this article, we've taken a detailed look at the end() function in PHP, including its syntax, usage, and common pitfalls. While it may seem like a simple function, there are some important nuances to be aware of when using it. By understanding how end() works, you can avoid common mistakes and use this function to its fullest potential in your PHP code.
Diagram
Here's a diagram to illustrate the usage of the end() function:
graph TD
A[Array] --> B(end())
B --> C{Check result}
C --> |True| D(Return last element)
C --> |False| E(Handle empty array)