PHP pos() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, pos() returns the value of the element the internal array pointer is on. It is an alias of current().

Because it is an alias, pos() works exactly like current(). The name current() is more common and easier to understand, so prefer it in new code. Neither function moves the pointer.

Syntax

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

PHP
$value = pos($letters);
CallWhat it does
pos($letters)Returns the value at the pointer without moving it.
current($letters)The main name for the same function.
pos($empty)Returns false when the array is empty.

pos() is an alias of current(). A new array starts with the pointer on the first element. Use next(), prev(), reset() and end() to move the pointer.

Library declarations
PHP
pos(array|object $array): mixed

Parameters

ParameterDescription
arrayRequired. The array to read the current value from.

Return Value

Return ValueDescription
mixedThe value at the pointer, or false if the array is empty or the pointer is beyond the last element.

PHP Examples

Example 1 — Read the Value at the Pointer

pos() reads the value, and next() moves the pointer so that pos() gives a different value.

PHP
<?php
$letters = ["x", "y", "z"];

echo pos($letters), PHP_EOL;

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

How It Works

  1. The pointer starts on the first element, so pos() returns "x".
  2. pos() does not move the pointer.
  3. next() moves the pointer forward by one element.
  4. pos() now returns "y".

Example 2 — pos() on an Empty Array

With no elements, there is nothing for the pointer to point at.

PHP
<?php
$empty = [];

var_dump(pos($empty));

How It Works

  1. $empty has no elements.
  2. pos() cannot find a current element.
  3. It returns false instead of giving an error.
  4. var_dump() prints bool(false).

Example 3 — pos() Is the Same as current()

Both names give identical results, and both follow the pointer after end().

PHP
<?php
$letters = ["x", "y", "z"];

next($letters);
var_dump(pos($letters) === current($letters));

end($letters);
echo pos($letters), PHP_EOL;

How It Works

  1. next() moves the pointer to "y".
  2. pos() and current() both return "y", so === gives true.
  3. end() moves the pointer to the last element.
  4. pos() now returns "z".

Frequently Asked Questions

It returns the value of the array element that the internal pointer points at. It does not move the pointer.
Yes. pos() is an alias of current() and behaves exactly the same.
Use current(). It is the main name, and readers will understand it faster.
No. Use next(), prev(), reset() or end() to move it.
Either the array is empty or the pointer is past the last element. A stored false value can also cause it.

Next: prev()

prev() moves the internal array pointer one step backward and returns the value of the new.

prev() 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