current

Description

Every array has an internal pointer to its "current" element,
which is initialized to the first element inserted into the
array.

Parameters

array

The array.

Return Values

The current() function simply returns the
value of the array element that's 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, current() returns FALSE.

Warning

This function may
return Boolean FALSE, but may also return a non-Boolean value which
evaluates to FALSE. Please read the section on Booleans for more
information. Use the ===
operator for testing the return value of this
function.

Changelog

Version

Description

7.0.0

array is now always passed by value.
Prior to this version, it was passed by reference if possible,
and by value otherwise.

Notes

Note:
The end of an array and the result of calling
current() on an empty array
are indistinguishable from a booleanFALSE element.
To properly traverse an array which may contain FALSE elements, see the
foreach() function.
To still use current() and properly check if the value
is really an element of the array, the key()
of the current() element should be checked to be strictly
different from NULL.

If you do current() after using uset() on foreach statement, you can get FALSE in PHP version 5.2.4 and above.There is example:<?php$prices = array(0 => '1300990',1 => '500',2 => '600');foreach($prices as $key => $price){ if($price < 1000){ unset($prices[$key]); }}

var_dump(current($prices)); // bool(false)?>If you do unset() without foreach? all will be fine.<?php$prices = array(0 => '1300990',1 => '500',2 => '600');unset($prices[1]);unset($prices[2]);

Based on this example http://php.net/manual/en/function.current.php#116128 i would like to add the following. As Vasily points out in his example <?php$prices = array(0 => '1300990',1 => '500',2 => '600');foreach($prices as $key => $price){ if($price < 1000){ unset($prices[$key]); }}

var_dump(current($prices)); // bool(false)?>The above example will not work and return false for version of PHP between 5.2.4 and 5.6.29. The issue is not present on PHP versions >= 7.0.1A different workaround (at least from Vasily's example) would be to use reset() before using current() in order to reset the array pointer to start.<?php$prices = array(0 => '1300990',1 => '500',2 => '600');foreach($prices as $key => $price){ if($price < 1000){ unset($prices[$key]); }}reset($prices);var_dump(current($prices)); // string(7) "1300990"?>

The docs do not specify this, but adding to the array using the brackets syntax:
<?php $my_array[] = $new_value; ?>
will not advance the internal pointer of the array. therefore, you cannot use current() to get the last value added or key() to get the key of the most recently added element.

You should do an end($my_array) to advance the internal pointer to the end ( as stated in one of the notes on end() ), then

It took me a while to figure this out, but there is a more consistent way to figure out whether you really went past the end of the array, than using each().

You see, each() gets the value BEFORE advancing the pointer, and next() gets the value AFTER advancing the pointer. When you are implementing the Iterator interface, therefore, it's a real pain in the behind to use each().

And thus, I give you the solution:To see if you've blown past the end of the array, use key($array) and see if it returns NULL. If it does, you're past the end of the array -- keys can't be null in arrays.

Nifty, huh? Here's how I implemented the Iterator interface in one of my classes:

As Sergey stated a long time ago in this notes, when adding to the array using the brackets syntax, current won't return the last added value. Instead of moving the pointer with end(), you simply can get the last value with <?php $my_array[count($my_array) - 1] ?> (only applies to a non-associative array).