Home  >  Article  >  Backend Development  >  PHP points the array internal pointer to the last element and returns the value of that element (if successful)

PHP points the array internal pointer to the last element and returns the value of that element (if successful)

WBOY
WBOYforward
2024-03-21 13:22:171040browse

This article will explain in detail about PHPpointing the internal pointer of the array to the last element and returning the value of the element (if successful). I think it is quite practical. , so I share it with you as a reference. I hope you can gain something from reading this article.

PHP points the internal pointer of the array to the last element

In php, you can use the following method to point the array internal pointer to the last element:

end($array);

This function moves the internal pointer to the last element of the array and returns the value of that element. If the array is empty, NULL is returned.

Example:

$array = ["foo", "bar", "baz"];

echo end($array); // Output: "baz"

Return the value of the element (if successful)

If the internal pointer is successfully moved to the last element, the end() function will return the value of that element. Otherwise, it returns NULL.

Example:

$array = ["foo", "bar", "baz"];

if (end($array) !== null) {
echo "Internal pointer is at the last element: " . end($array);
} else {
echo "Array is empty.";
}

Notice:

  • end() The function does not change the array itself. It only moves the internal pointer.
  • If the array is empty, the end() function will return NULL.
  • You can use the reset() function to reset the internal pointer to the first element before moving it.

The above is the detailed content of PHP points the array internal pointer to the last element and returns the value of that element (if successful). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete