Using end() to Move an Array Pointer to the Last Element in PHP

This article explains how the PHP end() function moves an array's internal pointer to its last element, describes its parameter requirements, return values, and provides a clear example demonstrating its usage and output.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Using end() to Move an Array Pointer to the Last Element in PHP

The end() function in PHP moves the internal pointer of an array to the last element and returns that element's value.

Function signature: mixed end (array &$array) Parameter description:

array – The array to be processed. It is passed by reference because the function modifies the internal pointer, so you must provide a real variable, not a temporary return value.

Return value: The value of the last element, or FALSE if the array is empty.

Example:

<?php
$fruits = array('apple', 'banana', 'cranberry');
echo end($fruits);
?>

Output: // cranberry This demonstrates that after calling end($fruits), the pointer is positioned at the last element ( 'cranberry') and that value is printed.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendprogrammingPHPArrayend-function
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.