PHP current() Function
In this chapter you will learn:
- Definition for PHP current() Function
- Syntax for PHP current() Function
- Parameter for PHP current() Function
- Note for PHP current() Function
- Related methods for PHP current() Function
- Example - Find out the current element
- Example - A demonstration of all related methods
Definition
The current() function returns the value of the current element in an array.
Every array has an internal pointer to its "current" element, which is initialized to the first element inserted into the array.
Syntax
PHP current() Function has the following syntax.
current(array)
Parameter
Parameter | Is Required | Description |
---|---|---|
array | Required. | Array to use |
Note
This function does not move the arrays internal pointer.
Related methods
Method | Description |
---|---|
end() | moves the pointer to, and outputs, the last element in the array |
next() | moves the pointer to, and outputs, the next element in the array |
prev() | moves the pointer to, and outputs, the previous element in the array |
reset() | moves the pointer to the first element of the array |
each() | returns the current element key and value, and moves the pointer forward |
Example 1
Find out the current element
<?php//from ja v a 2 s .c om
$people = array("A", "B", "C", "D");
echo current($people) . "\n";
?>
The code above generates the following result.
Example 2
A demonstration of all related methods:
<?php//from ja v a2 s . c o m
$people = array("A", "B", "C", "D");
echo current($people) . "\n"; // The current element is A
echo next($people) . "\n"; // The next element of A is B
echo current($people) . "\n"; // Now the current element is B
echo prev($people) . "\n"; // The previous element of B is A
echo end($people) . "\n"; // The last element is D
echo prev($people) . "\n"; // The previous element of D is C
echo current($people) . "\n"; // Now the current element is C
echo reset($people) . "\n"; // Moves the internal pointer to the first element of the array, which is A
echo next($people) . "\n"; // The next element of A is B
print_r (each($people)); // Returns the key and value of the current element (now B), and moves the internal pointer forward
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP each() Function
- Syntax for PHP each() Function
- Parameter for PHP each() Function
- Related methods for PHP each() Function
- Return value from each() function
- Note for PHP each() Function
- Example - Output current element
- Example - A loop to output the whole array
- Example - A demonstration of all related methods
Home » PHP Tutorial » PHP Array Functions