current
Returns the value the array's internal pointer is on, without moving the pointer.
current(array|object $array): mixed- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array|object | The array to read from. Not modified — the pointer is not advanced. |
Return value
mixedThe value at the internal pointer's current position, or false if the pointer is past the end of the array or the array is empty.
Errors
Does not throw. Emits a deprecation notice since PHP 8.1.0 when called on an object.
Changelog
- PHP 8.1.0Calling this function on objects is deprecated. Convert the object with get_mangled_object_vars() first, or use a class implementing Iterator such as ArrayIterator.
- PHP 7.4.0Instances of SPL classes are now treated like empty objects with no properties instead of calling the Iterator method of the same name.
Every PHP array carries an internal pointer. current reads the value at that pointer and leaves it exactly where it was — it is the only one of the pointer functions that does not move anything. pos is an alias of it.
When to reach for it
Two situations justify it in modern code. The first is hand-driven traversal with next, prev and reset, typically when two arrays must be walked in step. The second is grabbing the first value of an array without knowing its key: current($array) after a reset, though array_key_first plus a lookup or reset($array) itself is usually clearer.
What trips people up
The false return is ambiguous. An array containing false, 0, null or '' makes while ($v = current($a)) stop early, and there is no way to tell "the value is falsy" from "we ran off the end". Test key($array) === null instead — the key is null only when the pointer is genuinely past the end.
The pointer is part of the array's value, so copying an array copies its pointer position, and every sort function resets it. foreach in PHP 7 and later operates on its own copy of the pointer, so it no longer disturbs the one current reads.
Calling current on an object is deprecated as of PHP 8.1.
Example
When to use it
- Walk two parallel arrays in lockstep with next() while reading each with current().
- Read the first value of an associative array whose key you do not know.
- Implement a manual cursor over a small result set without a foreach.
Frequently asked questions
What is the difference between current and reset?
reset moves the internal pointer back to the first element and returns that element's value, while current returns the value at wherever the pointer already is without moving it. Call reset first if you need to be sure you are at the start.
Does foreach change what current returns?
No, not since PHP 7. foreach iterates over its own copy of the array pointer, so the position current reads is unaffected by a foreach loop over the same array.
How do I get the first element of an array in PHP?
Use array_key_first($array) and index with the result, or reset($array) if moving the pointer is acceptable. Both handle empty arrays predictably, and array_key_first avoids the false-versus-falsy ambiguity of current.
More examples
Detecting the end safely
Testing key() !== null visits all three elements, including the false in the middle, and stops only when the pointer really runs out. The naive while ($v = current($flags)) form would have quit after the first element.
<?php
$flags = [true, false, true]; // a legitimate false in the data
reset($flags);
while (key($flags) !== null) {
var_dump(current($flags));
next($flags);
}Walking two arrays in step
foreach drives the first array while the internal pointer of the second is advanced by hand, pairing ada with 91, grace with 88 and linus with 74. Since PHP 7 foreach no longer moves the pointer of the array it iterates, so this pattern is stable.
<?php
$names = ['ada', 'grace', 'linus'];
$scores = [91, 88, 74];
reset($scores);
foreach ($names as $name) {
echo $name, ': ', current($scores), "\n";
next($scores);
}First value without knowing the key
All three print dark, but only current() leaves the pointer untouched — reset() rewinds it and array_key_first ignores the pointer entirely. Prefer array_key_first in new code because it does not depend on hidden state.
<?php
$settings = ['theme' => 'dark', 'lang' => 'en'];
echo current($settings), "\n"; // dark
echo $settings[array_key_first($settings)], "\n"; // dark, no pointer involved
echo reset($settings), "\n"; // dark, and rewinds
Discussion