PHP Function Reference

PHP current() Function



The PHP current() function returns the current element of the array. Every array in PHP has an internal pointer to its "current" element, which is initialized to the first element inserted into the array.

Syntax

current(array)

Parameters

array Required. Specify the input array.

Return Value

Returns the value of the array element which is currently being pointed to by the internal pointer. It does not move the pointer in any way. If the internal pointer points beyond the end of the elements list or the array is empty, it returns false.

Note: This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Therefore, use === operator for testing the return value of this function.

Exceptions

NA.

Example:

The example below shows the usage of current() function.

<?php
$Arr = array(10, 20, "Hello", "World");

echo current($Arr)."\n";   //prints 10
echo next($Arr)."\n";      //prints 20
echo current($Arr)."\n";   //prints 20
echo end($Arr)."\n";       //prints World
echo current($Arr)."\n";   //prints World

echo "\n";
$Arr1 = array();
var_dump(current($Arr1));  //bool(false)

$Arr2 = array(array());
var_dump(current($Arr2));  //array(0) { }
?>

The output of the above code will be:

10
20
20
World
World

bool(false)
array(0) { }

Example:

Consider one more example where the current() function is used with an associative array.

<?php
$Arr = array(10=>"Red",
             20=>"Green",
             30=>"Blue",
             40=>"Black",
             50=>"White");

echo current($Arr)."\n";   //prints Red
echo next($Arr)."\n";      //prints Green
echo current($Arr)."\n";   //prints Green
echo end($Arr)."\n";       //prints White
echo current($Arr)."\n";   //prints White
?>

The output of the above code will be:

Red
Green
Green
White
White

❮ PHP Array Reference