current()
IntroductionThe use of arrays in PHP can be a powerful tool for developers to manage and manipulate data. One such array function is "current," which
Introduction
Every PHP array carries an internal pointer — a hidden cursor that marks one element as the "current" one. The current() function reads the value at that cursor without moving it. This makes current() the read half of PHP's manual array-traversal toolkit, the counterpart to the navigation functions next(), prev(), reset(), and end().
This chapter explains exactly what the internal pointer is, how current() behaves at the edges of an array, and the gotchas worth knowing before you reach for it.
What Is the current() Function in PHP?
current() returns the value of the element the array's internal pointer is currently on. It does not advance the pointer, so calling it repeatedly returns the same value until something else moves the cursor.
When a fresh array is created, the pointer starts on the first element. The functions that move it are:
next()— move one element forward, then return that value.prev()— move one element back, then return that value.reset()— jump to the first element.end()— jump to the last element.
current() is the only one of these that reads without moving.
Syntax of the current() Function
current(array $array): mixedIt takes a single argument — the array to inspect — and returns the value at the current pointer position, or false if the array is empty or the pointer has run past the last element.
Caution: Because
current()returnsfalseat the end of an array, you cannot use it to detect the end of an array that legitimately contains afalsevalue. Usekey(), which returnsnullpast the end, when you need a reliable end-of-array check.
Basic Example
The pointer starts on the first element, so this prints apple. Call current() ten more times and you still get apple — nothing has moved the cursor.
Moving the Pointer with next() and prev()
current() becomes useful in combination with the navigation functions. Each one repositions the cursor, and current() (or the value the function itself returns) tells you where you landed.
<?php
$fruits = ['apple', 'banana', 'cherry'];
echo current($fruits) . "\n"; // apple (pointer at index 0)
next($fruits);
echo current($fruits) . "\n"; // banana (pointer at index 1)
next($fruits);
echo current($fruits) . "\n"; // cherry (pointer at index 2)
prev($fruits);
echo current($fruits) . "\n"; // banana (pointer back at index 1)
reset($fruits);
echo current($fruits) . "\n"; // apple (pointer reset to start)The output is:
apple
banana
cherry
banana
appleWhat Happens at the End of the Array
Step the pointer past the final element and current() returns false:
<?php
$fruits = ['apple', 'banana', 'cherry'];
end($fruits); // pointer on the last element
echo current($fruits) . "\n"; // cherry
next($fruits); // step beyond the last element
var_dump(current($fruits)); // bool(false)This prints:
cherry
bool(false)This is why the caution above matters: a false from current() can mean "off the end" or "the element's value really is false."
current() and foreach
A common surprise: foreach iterates over an array without using or moving its internal pointer. So current() is not affected by a foreach loop and still reflects whatever the pointer-moving functions last set.
<?php
$fruits = ['apple', 'banana', 'cherry'];
foreach ($fruits as $fruit) {
// looping does not touch the internal pointer
}
echo current($fruits); // apple — still at the startThis prints apple, not false. If you want manual control over position, use next()/current(); if you just want to visit every element, use foreach.
When to Use current()
Reach for current() when you need a stateful walk through an array — one where you advance the cursor across several function calls or loop iterations and need to peek at the active element. Typical cases:
- Processing a queue-like array while juggling
next()andprev(). - Reading the first element cheaply:
current($array)returns the first value without needing its key. - Implementing a custom cursor over a dataset.
For everyday iteration, prefer foreach — it is clearer and immune to pointer surprises.
Conclusion
current() reads the value at an array's internal pointer without moving it, making it the read half of PHP's manual traversal set alongside next(), prev(), reset(), and end(). Remember its two edges: it returns the same value until something else moves the cursor, and it returns false past the end of the array — so use key() when you need to detect the end reliably.