Content · Programming · PHP

PHP end() Function: Usage and Examples

Learn how PHP end works with practical examples, important behavior, relevant alternatives and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

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

FunctionPurpose
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

MistakeCorrect Approach
Ignoring this behavior: end() changes the internal pointerend() 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.

Share this resourceSend this page to someone who may find it useful.