Current Location: Home> Latest Articles> Comparison of PHP's end() Function and Laravel's last() Method: Usage, Differences, and Applicable Scenarios

Comparison of PHP's end() Function and Laravel's last() Method: Usage, Differences, and Applicable Scenarios

M66 2025-06-23

In PHP and Laravel framework development, end() and last() are two commonly used functions that operate on the last element of an array. While their functionality is similar, they differ in syntax, features, and applicable scenarios in practical use. This article will provide a detailed explanation of the usage, differences, and respective applicable scenarios of these two functions.

1. The end() Function in PHP

The end() function in PHP moves the internal pointer of an array to its last element and returns the value of that element. end() is typically used to retrieve the last element of an array.

Syntax:

mixed end(array &$array)

Parameters:

  • $array: The array passed in. The end() function will modify the internal pointer of this array to point to its last element.

Example:

<?php
$array = [1, 2, 3, 4, 5];
$lastElement = end($array);  // Get the last element
echo $lastElement;  // Output: 5
?>

Notes:

  • The end() function not only returns the last element in the array but also changes the internal pointer of the array to that element. Therefore, if you call functions like current() afterwards, they will return the last element instead of the first.

  • If the array is empty, end() returns false.

2. The last() Method in Laravel

In Laravel, last() is a method of the Collection class that returns the last element in the collection. This method is similar in purpose to PHP’s end(), but last() is specifically designed for Laravel collection objects and offers greater flexibility and readability.

Syntax:

mixed last(callable $callback = null)

Parameters:

  • $callback: An optional callback function used to filter the collection’s elements. If provided, last() will return the last element in the collection that satisfies the callback condition.

Example:

<?php
use Illuminate\Support\Collection;
<p>$collection = collect([1, 2, 3, 4, 5]);<br>
$lastElement = $collection->last();  // Get the last element<br>
echo $lastElement;  // Output: 5<br>
?><br>

Example with Callback:

<?php
use Illuminate\Support\Collection;
<p>$collection = collect([1, 2, 3, 4, 5]);<br>
$lastEvenElement = $collection->last(function ($value) {<br>
return $value % 2 == 0;  // Find the last even number<br>
});<br>
echo $lastEvenElement;  // Output: 4<br>
?><br>

Notes:

  • Unlike PHP’s end(), last() is a method for Laravel’s Collection class. It supports method chaining and accepts a callback to filter elements.

  • If no elements match the callback condition, last() returns null.

3. Differences Between end() and last()

Although both end() and last() are used to retrieve the last element, their differences can be summarized in the following aspects:

1. Applicable Scenarios:

  • end() is a built-in PHP function suitable for any PHP array. It is very basic and does not depend on the Laravel framework.

  • last() is a method of the Laravel Collection class, designed for Laravel collection objects rather than plain PHP arrays. It integrates better with other collection operations through chaining and supports filtering via callbacks.

2. Functionality:

  • end() not only returns the last element of an array but also changes the array’s internal pointer, affecting subsequent array operations (e.g., current()).

  • last() does not alter the collection’s pointer and only returns the last element, preserving the integrity of the collection without pointer modifications.

3. Flexibility:

  • last() supports callback functions to return the last element that meets a specific condition. end() does not have this capability.

4. Readability and Chaining:

  • last() supports method chaining and works well with other Laravel collection methods, resulting in cleaner and more readable code. end() cannot be chained and is generally used in simpler scenarios.

4. Applicable Use Cases

1. When to Use end():

  • If you only need to perform simple operations on a PHP array, such as getting its last element, and do not care about pointer changes, end() is a straightforward and effective choice.

  • Example Scenario: Use end() when directly manipulating arrays in non-Laravel projects.

2. When to Use last():

  • In Laravel projects, especially when working with Collections, last() offers greater flexibility and readability. It not only retrieves the last element but also supports filtering via callbacks, suitable for more complex scenarios.

  • Example Scenario: Use last() when handling query results or data collections in Laravel to get the last element that meets a condition.

Conclusion

end() and last() are similar in function, both used to obtain the last element from an array or collection. However, they differ significantly in applicable scenarios, flexibility, and code readability. For native PHP array operations, the end() function is simple and efficient; whereas in the Laravel framework, the last() method is more powerful, supporting method chaining and callback filtering, making it suitable for more complex data processing tasks.

No matter whether you use PHP or Laravel, understanding their differences will help you choose the most appropriate tool according to your specific needs.