Home  >  Article  >  Backend Development  >  Detailed explanation of usage examples of php end() function and current() function

Detailed explanation of usage examples of php end() function and current() function

怪我咯
怪我咯Original
2017-06-06 11:42:133345browse

What are the functions of php end() function and current() function?

The end() function in php points the internal pointer of the array to the last element and returns the value of the element, while the current() function returns the current element (unit, which is the array) in the array. The first element. One of these two functions returns the first element of the array, and the other returns the last element of the array. Let’s take a look at the usage of these two.

end() function

The end() function points the internal pointer of the array to the last element, and returns the value of the element if successful;

Example 1

Use the end() function to return the last element of the array, the code is as follows

<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo end($people);
?>

Code running results:

Detailed explanation of usage examples of php end() function and current() function

current() function

current() function returns the current element (unit) in the array. Each array has an internal pointer pointing to its "current" element, initially pointing to the first element inserted into the array. Element.

PS: If there is an empty element, or the element has no value, this function also returns FALSE

Use current(. ) function returns the first element of the array, the code is as follows

<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($people);
?>

Code running result:

Detailed explanation of usage examples of php end() function and current() functionExample 3

Use the current() function and end() function to output the value of the current element and the last element in the array. The code is as follows

<?php
$people = array("Bill", "Steve", "Mark", "David");

echo current($people) . "<br>";
echo end($people);
?>

The code running result:

Detailed explanation of usage examples of php end() function and current() function【Recommended related articles】

php Definition and usage of array function current()

php Operation array function current Detailed explanation of usage examples of next and reset functions

The above is the detailed content of Detailed explanation of usage examples of php end() function and current() function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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