PHP reset() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, reset() moves the internal array pointer back to the first element and returns the value of that element.

After you walk through an array with next() or end(), reset() takes the pointer back to the start. It is also a short way to read the first value of an array.

Syntax

Call reset() with the values you need. Start with this line.

PHP
$first = reset($colors);
CallWhat it does
reset($colors)Moves the pointer to the first element and returns its value.
reset($empty)Returns false when the array has no elements.
key($colors)Reads the first key after the pointer has been reset.

reset() takes the array by reference, so you must pass a variable. It changes only the pointer and never the contents of the array.

Library declarations
PHP
reset(array|object &$array): mixed

Parameters

ParameterDescription
arrayRequired. The array whose internal pointer moves to the first element. It is passed by reference.

Return Value

Return ValueDescription
mixedThe value of the first element, or false if the array is empty.

PHP Examples

Example 1 — Go Back to the First Element

After moving the pointer forward, reset() brings it back to the start.

PHP
<?php
$letters = ["a", "b", "c"];

next($letters);
next($letters);
echo current($letters), PHP_EOL;

echo reset($letters), PHP_EOL;

How It Works

  1. The two next() calls move the pointer to the third element.
  2. current() reads that element, so "c" is printed.
  3. reset() moves the pointer back to the first element.
  4. reset() returns the first value, "a", and echo prints it.

Example 2 — reset() on an Empty Array

An empty array has no first element, so reset() returns false.

PHP
<?php
$empty = [];

var_dump(reset($empty));

How It Works

  1. $empty contains no elements.
  2. reset() looks for a first element and finds none.
  3. It returns false and does not show an error.
  4. var_dump() prints bool(false).

Example 3 — Get the First Value of an Array with Keys

reset() ignores the keys and returns the first value, whatever its key is. key() then tells you that key.

PHP
<?php
$levels = ["x" => 10, "y" => 20];

end($levels);

echo reset($levels), PHP_EOL;
echo key($levels), PHP_EOL;

How It Works

  1. end() moves the pointer to the last element, so the pointer is away from the start.
  2. reset() moves it back and returns the first value, 10.
  3. key() reads the key at the pointer, which is "x".
  4. This is a common way to read the first value of an associative array.

Frequently Asked Questions

It moves the internal pointer of an array to the first element and returns the value of that element, or false if the array is empty.
No. It only changes the hidden pointer. The values and keys remain the same.
reset($array) returns it. On PHP 7.3 and later you can also use $array[array_key_first($array)], which does not move the pointer.
reset() goes to the first element. end() goes to the last element.
No. foreach starts at the beginning by itself and does not use the internal pointer. reset() matters only when you use next(), prev() or current().

Next: rsort()

rsort() sorts the values of an array from highest to lowest and gives them new keys 0, 1, .

rsort() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful