Get the last element of an array in PHP – end()

The end() function is an inbuilt function in PHP Programming. The purpose of this function is to find the last element of the given input array. The end() function changes the internal pointer of an array to the last element and returns the value of the last element.

end() PHP Function available in PHP version 4, PHP version 5, PHP version 7

Return value

On Success:

The end() function returns the value of the last element of the array on success

On Failure:

FALSE on failure i.e, when the array is empty.

Syntax of end() PHP function

end ( array $array )

Sample code with end() PHP function

<?php

$source_array = ['key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'];

$result = end($source_array);

echo "Last element: ".$result;

?>
Gets the last element of an array in PHP – end()

Output / Result of end() PHP function

Last element: value3

You Might Also Like

2 Replies to “Get the last element of an array in PHP – end()”

  1. Hi there, the whole thing is going perfectly here
    and of course every one is sharing data, that’s actually excellent, keep up writing.

  2. I do believe all of the ideas you have introduced on your post.

    They are really convincing and will certainly work. Nonetheless, the posts are too short for beginners.
    Could you please extend them a little from subsequent time?

    Thanks for the post.

Leave a Reply

Your email address will not be published. Required fields are marked *