Current Location: Home> Latest Articles> Comparison of usage scenarios with end() and array_key_last()

Comparison of usage scenarios with end() and array_key_last()

M66 2025-06-02

In PHP, end() and array_key_last() are commonly used array operation functions, and they have different functions and usage scenarios. Today we will discuss the differences between these two functions to help you better understand their use in PHP programming.

1. end() function

The end() function is a very common function in PHP that points an internal pointer to the last element of the array and returns the value of that element. If the array is empty, false is returned.

Sample code:

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

// use end() Get the value of the last element
$lastElement = end($array);
echo $lastElement;  // Output cherry
?>
  • end() changes the position of the pointer inside the array, which means it will affect the operation of the pointer in the array.

  • end() returns the value of the array, not the key name.

Applicable scenarios:

  • When you need to quickly access the last element value of an array, end() is a very convenient option.

  • But be aware that end() will change the pointer state of the array and may affect the return value of other functions, such as current() or key() .

2. array_key_last() function

array_key_last() is a new function introduced in PHP 7.3.0 to get the key name of the last element of the array. Unlike end() that returns a value, array_key_last() returns a key of the last element of the array.

Sample code:

 <?php
$array = ["apple" => "red", "banana" => "yellow", "cherry" => "red"];

// use array_key_last() Get the key name of the last element
$lastKey = array_key_last($array);
echo $lastKey;  // Output cherry
?>
  • array_key_last() only returns the key name of the last element of the array and does not change the pointer of the array.

  • If the array is empty, array_key_last() returns null .

Applicable scenarios:

  • array_key_last() is a very suitable choice when you need to know the key name of the last element in the array.

  • Unlike end() , array_key_last() does not change the internal pointer of the array, so it does not affect other array operations.

The difference between end() and array_key_last()

characteristic end() array_key_last()
Return value The value of the last element of the array The key name of the last element of the array
Whether to change the pointer Change array pointer Don't change the array pointer
Use scenarios Use when accessing values ​​are required Use when accessing key names are required
PHP Version PHP 4+ PHP 7.3+

Summarize

  • end() : When you need to get the value of the last element of the array, use end() .

  • array_key_last() : When you need to get the key name of the last element of the array, use array_key_last() .

The choice of both depends on your needs, whether the array value or key name is required, and whether it will affect the pointer state of the array. Understanding their differences can help you make more appropriate choices in different programming scenarios.