Moves an array’s internal pointer to the final element and returns that value.
What Is end() in PHP?
Moves an array’s internal pointer to the final element and returns that value.
Its syntax is:
end(array|object &$array): mixed
Parameters:
$array— Accepted as array|object &; required argument.
Return type: mixed. end() changes the internal pointer. Empty arrays return false, which may also be an actual stored value.
Availability: Core PHP.
Example 1: Basic Usage
<?php
$items = ["first", "last"]; echo end($items);
Output:
last
End() changes the internal pointer. Empty arrays return false, which may also be an actual stored value.
Example 2: A Different Input
<?php
$prices = [8, 13, 21]; echo end($prices);
Output:
21
End() changes the internal pointer. Empty arrays return false, which may also be an actual stored value.
Practical Example: Applying It in a Program
<?php
$events = ["created", "queued", "finished"]; $mostRecentEvent = end($events); echo $mostRecentEvent;
Output:
finished
End() changes the internal pointer. Empty arrays return false, which may also be an actual stored value.
Important Behavior and Edge Cases
- Key behavior: end() changes the internal pointer. Empty arrays return false, which may also be an actual stored value.
- Environment: Core PHP.
Comparison With Related Functions
| Function | Purpose |
|---|---|
end() | Moves an array’s internal pointer to the final element and returns that value. |
reset() | end() moves the array pointer to the last item; reset() moves it to the first. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this behavior: end() changes the internal pointer | end() changes the internal pointer. Empty arrays return false, which may also be an actual stored value. |
Key Takeaways
- Moves an array’s internal pointer to the final element and returns that value.
- end() changes the internal pointer. Empty arrays return false, which may also be an actual stored value.
- Core PHP.
Summary: end() changes the internal pointer. Empty arrays return false, which may also be an actual stored value.