current()
function in PHP to get the first element of the array (which in fact is not its functionality since it returns the current pointer in array is) and I've been noticing something that has left me a bit confused compared to the features of next()
, prev()
and reset()
.
And I explain why.
In PHP manual and even in Notepad ++ , we see that the argument of current()
is a reference passage of an element of type array
, in the same way as next()
and prev()
.
In fact, the function declaration is this:
mixed current ( array &$array )
However, if you use it in a array returned function or even a temporary array (in this case, declared directly as current()
argument) works normally (the argument would be a variable passed by reference).
current([1, 2, 3]);// 1
current(explode(',', '1, 2, 3')); //1
$array = [1, 2, 3];
current($array); //1
Now, what makes my confusion even greater is that if I pass a array directly into functions like next()
and prev()
, this causes a fatal error:
next([1, 2, 3]);
//Fatal error: Only variables can be passed by reference
echo next(explode(',', '1, 2, 3'));
//Strict Standards: Only variables should be passed by reference
The question is: If the PHP manual specifies in the function declaration that current()
works with arrays
passed by reference (like many other functions used for arrays
), why is it the only one that works with arrays passed directly as an argument (as in the first example)?
Note : The PHP Manual is very emphatic: mixed current ( array &$array )