PHP array_last() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_last() returns the last value of an array, or null if the array is empty.

It does not change the array or its pointer. This function is available in PHP 8.5 and newer.

Syntax

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

PHP
$last = array_last($items);
CallWhat it does
array_last([1, 2, 3])Returns 3.
array_last(["a" => 1, "b" => 2])Returns 2, the last value.
array_last([])Returns null for an empty array.

This function needs PHP 8.5. On older versions use $array[array_key_last($array)] for the same result.

Library declarations
PHP
array_last(array $array): mixed

Parameters

ParameterDescription
arrayRequired. The array whose last value you want.

Return Value

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

PHP Examples

Example 1 — Get the Last Value

Read the final item of a list.

PHP
<?php
$colors = ["red", "green", "blue"];

echo array_last($colors), PHP_EOL;

How It Works

  1. Create a list. The last item is blue.
  2. Call array_last(). It returns the value at the end.
  3. Print it. The output is blue.
  4. The array is not changed. Nothing is removed, unlike array_pop().

Example 2 — Empty Arrays and Named Keys

See the result for an empty array and for an associative array.

PHP
<?php
echo var_export(array_last([]), true), PHP_EOL;
echo array_last(["a" => 1, "b" => 2]), PHP_EOL;
echo array_last([5 => "x", 1 => "y"]), PHP_EOL;

How It Works

  1. Use an empty array. There is no last value, so the result is null.
  2. Use named keys. The last value is 2, no matter what the key is called.
  3. Use unordered numeric keys. The last added item is y.
  4. Remember the order. It is the last item in the array order, not the biggest key.

Example 3 — Work on Older PHP Versions

Check if the function exists and fall back to array_key_last() if not.

PHP
<?php
$versions = ["v1", "v2", "v3"];

$latest = function_exists("array_last")
    ? array_last($versions)
    : $versions[array_key_last($versions)];

echo "Latest: " . $latest . PHP_EOL;

How It Works

  1. Create the list. The final item is v3.
  2. Check function_exists(). This tells you if PHP 8.5 is running.
  3. Use the fallback if needed. array_key_last() gives the last key to read.
  4. Print the result. Both paths give v3.

Frequently Asked Questions

It returns the last value of an array, or null if the array is empty. The array is not changed.
It was added in PHP 8.5. It does not exist in earlier versions.
Use $array[array_key_last($array)], or end($array), though end() moves the internal pointer.
array_pop() removes the last item. array_last() only reads it and leaves the array unchanged.
array_first() returns the first value of an array in PHP 8.5.

Next: array_map()

array_map() applies a callback to every item and returns a new array of results.

array_map() 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