Home > Article > Backend Development > Detailed explanation of usage examples of php end() function and current() function
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:
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:
Example 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:
【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!