PHP end() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, end() moves the internal pointer of an array to its last element and returns the value of that element.

Every PHP array has a hidden pointer that points at one element. end() sends that pointer to the last element. It is the quickest way to read the last value of an array, and it also lets you walk backward with prev().

Syntax

Call end() with the values you need. Start with this line.

PHP
$last = end($fruits);
CallWhat it does
end($fruits)Moves the pointer to the last element and returns its value.
end($ages)Works with string keys too. Use key($ages) afterward to learn the last key.
end($empty)Returns false when the array has no elements.

end() takes the array by reference, so the pointer of your own variable is changed. You must pass a variable, not the result of a function call. The array values themselves are never changed.

Library declarations
PHP
end(array|object &$array): mixed

Parameters

ParameterDescription
arrayRequired. The array whose internal pointer should move to the last element. It is passed by reference.

Return Value

Return ValueDescription
mixedThe value of the last element, or false if the array is empty.

PHP Examples

Example 1 — Get the Last Value of an Array

The simplest use of end() is to read the final element of a list of values.

PHP
<?php
$fruits = ["apple", "banana", "cherry"];

$last = end($fruits);

echo $last;

How It Works

  1. The array $fruits holds three strings.
  2. end() moves the internal pointer to the last element, which is "cherry".
  3. The value of that element is returned and saved in $last.
  4. echo prints the value, so the page shows cherry.

Example 2 — Empty Arrays and String Keys

end() returns false for an empty array. For an array with string keys it still returns the last value, and key() tells you which key that was.

PHP
<?php
$empty = [];
var_dump(end($empty));

$ages = ["Tom" => 30, "Ann" => 25];
echo end($ages), PHP_EOL;
echo key($ages), PHP_EOL;

How It Works

  1. $empty has no elements, so there is no last element to return.
  2. var_dump() shows that end() gave back the boolean false.
  3. For $ages the last element is "Ann" => 25, so end() returns 25.
  4. key() reads the key at the pointer's new position, which is Ann.

Example 3 — end() Moves the Pointer and reset() Brings It Back

The internal pointer stays where end() left it. This example uses current() to watch the pointer and reset() to send it back to the start.

PHP
<?php
$numbers = [10, 20, 30];

echo current($numbers), PHP_EOL;

end($numbers);
echo current($numbers), PHP_EOL;

reset($numbers);
echo current($numbers), PHP_EOL;

How It Works

  1. A new array always starts with its pointer on the first element, so current() returns 10.
  2. end() moves the pointer to the last element. Its return value is ignored here.
  3. current() now returns 30, which proves the pointer really moved.
  4. reset() moves the pointer back to the first element, so current() returns 10 again.

Frequently Asked Questions

It moves the internal pointer of an array to the last element and returns that element's value. If the array is empty it returns false.
It does not change the values or the order of the elements. It only moves the hidden internal pointer, which is why the array is passed by reference.
Because end() needs a real variable to move the pointer in. If you write end(explode(",", $text)), PHP shows a notice. Save the result in a variable first, then call end() on that variable.
end() returns the last value and moves the pointer. array_key_last() returns the last key and does not touch the pointer. Use the one that matches what you need.
Use $array[array_key_last($array)] on PHP 7.3 or later. This reads the last value and leaves the internal pointer where it was.

Next: extract()

extract() turns the keys of an array into variables. Each key becomes a variable name and .

extract() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful