Home > Backend Development > PHP Tutorial > 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
Release: 2024-03-21 13:24:02
forward
1095 people have browsed it

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);
Copy after login

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"
Copy after login

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.";
}
Copy after login

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!

source:lsjlt.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template