PHP current() function
Example
Output the value of the current element in the array:
<?php $people = array("Bill", "Steve", "Mark", "David"); echo current($people) . "<br>"; ?>
Definition and usage
The `current()` function returns the value of the current element in the array.
Each array has an internal pointer that points to its 'current' element, initially pointing to the first element inserted into the array.
Tip:This function does not move the internal pointer of the array. To do this, please use next() And prev() Function.
Related methods:
- end() - Points the internal pointer to the last element in the array and outputs
- next() - Points the internal pointer to the next element in the array and outputs
- prev() - Points the internal pointer to the previous element in the array and outputs
- reset() - Points the internal pointer to the first element in the array and outputs
- each() - Returns the key name and value of the current element and moves the internal pointer forward
Syntax
current(array)
Parameter | Description |
---|---|
array | Required. Specifies the array to be used. |
Description
The `current()` function returns the current element (unit) in the array.
Each array has an internal pointer that points to its 'current' element, initially pointing to the first element inserted into the array.
The `current()` function returns the value of the array element pointed to by the internal pointer without moving the pointer. If the internal pointer points beyond the end of the unit list, `current()` returns FALSE.
Technical details
Return value: | Returns the value of the current element in the array, if the current element is empty or the current element has no value, then returns FALSE. |
PHP Version: | 4+ |
More Examples
Example 1
Demonstrate all related methods:
<?php $people = array("Bill", "Steve", "Mark", "David"); echo current($people) . "<br>"; // The current element is Bill echo next($people) . "<br>"; // Bill's next element is Steve echo current($people) . "<br>"; // Now the current element is Steve echo prev($people) . "<br>"; // Steve's previous element is Bill echo end($people) . "<br>"; // The last element is David echo prev($people) . "<br>"; // The element before David is Mark echo current($people) . "<br>"; // The current element is currently Mark echo reset($people) . "<br>"; // Moves the internal pointer to the first element of the array, that is, Bill echo next($people) . "<br>"; // Bill's next element is Steve print_r (each($people)); // Returns the key name and value of the current element (currently Steve) and moves the internal pointer forward ?>