PHP end() function

P
PHP each() function
PHP in_array() function

In this article, you will learn how to get the last element of an array in PHP. The end function moves the internal pointer of an array to the last element and also returns its value.

What is the syntax of the end function in PHP?

end(array)
ParameterDetails
arrayThe array to get the last element of – Required
PHP end function

Example of end function

Example 1. Get the last element of the array.

<?php
$members= array("Jawad", "Ahmad", "Sumerina");
echo end($members);
?>

Example 1. In this example, you will find all the methods related to the end method category. These methods work with the internal pointer of the arrays.

<?php
$members= array("Jawad", "Ahmad", "Sumerina", "ACS");

// Current element: Jawad
echo current($people) . "<br>";

// Next element of Jawad: Ahmad
echo next($people) . "<br>"; 

// Now the current element: Ahmad
echo current($people) . "<br>"; 

// Previous element of Ahmad: Jawad
echo prev($people) . "<br>"; 

// Last element: ACS
echo end($people) . "<br>"; 

// Previous element of ACS: Sumerina
echo prev($people) . "<br>"; 

// Current element: Sumerina
echo current($people) . "<br>"; 

// Move the internal pointer to the first element of the array:Jawad
echo reset($people) . "<br>"; 

// Next element of Jawad: Ahmad
echo next($people) . "<br>"; 

// Get key and value of Ahmad (current element) then moves the internal pointer forward
print_r (each($members));
?>
PHP each() function
PHP in_array() function

Add comment

Tutor Network

Share this

Learn PHP from A to Z