PHP array_last() Function

What is PHP array_last() Function?

If you want to get the last value of an array, use array_last() function.

Syntax:

array_last(array)

Parameters:

The Function has 1 required parameter-

array (Required): The input array whose last value you want to get.

Return Values:

The function returns-

  • The last element of the array
  • If the input array is empty, the function returns NULL.

Examples:

Example 1:

<?php
$language = ["PHP", "Python", "Java", "HTML", "CSS"];
$last_value = array_last($language);
echo "The last value of the array is: " . $last_value;
?>

Output:

The last value of the array is: CSS

Example 2:

<pre>
<?php
$language = [];
$last_value = array_last($language);
echo "The last value of the array is: ";
var_dump($last_value);
?>
</pre>

Output:

The last value of the array is: NULL

PHP Version Support:

PHP 8 >= 8.5.0

Summary: PHP array_last() Function

The array_last() function is a built-in PHP function and part of the PHP’s array functions. It is a useful function to get the last value of the array.

Reference:

https://www.php.net/manual/en/function.array-last.php