Arrays7 min readExamples run on PHP 8.5.10

current()

Learn how PHP current() reads the element at an array's internal pointer without moving it, and how to avoid the false-value trap.

Signature

current(array|object $array): mixed

Purpose

Every PHP array carries an internal pointer that marks one of its elements as the "current" one. A new array starts with the pointer on the first element inserted. current() reads the value at that position and leaves the pointer where it is. It is the read-only member of a small family of pointer functions: reset(), end(), next(), prev() and key().

Most modern code walks arrays with foreach, but current() is useful when you want manual, step-by-step control, for example when you peek at the first or last element, or when you build a small cursor-style reader over an in-memory list.

Signature

Parameters

  • $array: the array whose internal pointer is read. There is no default value, so the argument is required.

The signature also accepts an object, but calling current() on an object has been deprecated since PHP 8.1. If you need the properties of an object, convert it first with get_mangled_object_vars(), or use a class that implements Iterator, such as ArrayIterator. Treat the array form as the only supported way to use this function.

Return value and failure cases

current() returns the value of the element the pointer is on. The return type is mixed, because array elements can be of any type.

It returns false in two situations:

  1. The array is empty.
  2. The internal pointer has moved beyond the last element, for example after next() was called on the final element.

There is no exception or warning in either case; you just receive false. This creates the main trap of the function: an array may legitimately contain the value false, and then the return value cannot tell you whether you found a real element or ran off the end. Always compare with ===, and when false is a possible element, check key() as well. key() returns null when the pointer is not on a valid element.

Example 1: reading the pointer as it moves

The pointer only changes when you call a function that moves it. Calling current() repeatedly returns the same element.

Example
<?php
$items = ['foot', 'bike', 'car'];
echo current($items), PHP_EOL;
next($items);
echo current($items), PHP_EOL;
next($items);
next($items);
var_dump(current($items));
Output
foot
bike
bool(false)

The first call shows foot. After one next() the pointer is on bike. Two more calls to next() move it first to car and then past the end, so the final current() gives false.

Example 2: telling a false element from the end

When an array contains false, the value alone is ambiguous. The key settles the question.

Example
<?php
$flags = [false, true];
var_dump(current($flags));
var_dump(key($flags));
next($flags);
next($flags);
var_dump(current($flags));
var_dump(key($flags));
Output
bool(false)
int(0)
bool(false)
NULL

At the start both the element and a pointer-past-the-end would show bool(false), but key() returns int(0), proving the pointer is on a real element. After moving beyond the end, key() returns NULL. A check such as key($array) !== null is the reliable test for "the pointer is on an element".

Example 3: associative arrays, end() and reset()

current() works on string keys exactly as on integer keys. It reports the value; use key() for the matching key.

Example
<?php
$scores = ['ana' => 10, 'ben' => 20, 'cy' => 30];
$before = current($scores);
end($scores);
$afterEnd = current($scores);
$key = key($scores);
reset($scores);
$afterReset = current($scores);
echo json_encode([
    'before' => $before,
    'afterEnd' => $afterEnd,
    'key' => $key,
    'afterReset' => $afterReset,
]), PHP_EOL;
Output
{"before":10,"afterEnd":30,"key":"cy","afterReset":10}

The pointer starts on ana, jumps to cy after end(), and returns to ana after reset(). Reading with current() never caused any of those moves.

Edge cases

  • Empty array: returns false, and key() returns null.
  • Pointer past the end: returns false until you call reset() or end() to bring the pointer back. Calling prev() from there does not bring it back, because the pointer is already outside the array.
  • Nested arrays: if the current element is itself an empty array, you get that array, not false. The result is a value, so test it with === against false rather than with a truthiness check.
  • Objects: deprecated since PHP 8.1 as noted above. Convert to an array first.

Common mistakes

  • Using a loose check. if (current($a)) fails for 0, '', '0', null, [] and false, all of which can be valid elements. Use === false and, when needed, key().
  • Expecting current() to advance. It never moves the pointer. A while (current($a) !== false) loop without a next() call runs forever on a non-empty array.
  • Assuming the pointer is at the start. Earlier code may have moved the pointer. If you need the first element, call reset(), which also returns that element.
  • Using it where foreach is simpler. For a plain full traversal, foreach is clearer and handles false elements correctly.
  • reset() moves the pointer to the first element.
  • end() moves the pointer to the last element.
  • next() advances the pointer by one.
  • prev() moves the pointer back by one.
  • key() returns the key at the pointer, or null if there is none.
  • foreach iterates an array without manual pointer handling.

Recap

current() returns the value under the array's internal pointer and does not move it. It returns false for an empty array or when the pointer is past the end, which is indistinguishable from a stored false. Compare strictly, confirm with key() !== null when it matters, and prefer foreach for ordinary loops.

Tested examples

Reading the pointer as it moves
<?php
$items = ['foot', 'bike', 'car'];
echo current($items), PHP_EOL;
next($items);
echo current($items), PHP_EOL;
next($items);
next($items);
var_dump(current($items));
Output
foot
bike
bool(false)
Telling a false element from the end
<?php
$flags = [false, true];
var_dump(current($flags));
var_dump(key($flags));
next($flags);
next($flags);
var_dump(current($flags));
var_dump(key($flags));
Output
bool(false)
int(0)
bool(false)
NULL
Associative arrays with end() and reset()
<?php
$scores = ['ana' => 10, 'ben' => 20, 'cy' => 30];
$before = current($scores);
end($scores);
$afterEnd = current($scores);
$key = key($scores);
reset($scores);
$afterReset = current($scores);
echo json_encode([
    'before' => $before,
    'afterEnd' => $afterEnd,
    'key' => $key,
    'afterReset' => $afterReset,
]), PHP_EOL;
Output
{"before":10,"afterEnd":30,"key":"cy","afterReset":10}