php – How does the current function work?

Question:

It's been a while since I've always used the current() function in PHP to get the first element of the array (which actually isn't its functionality, as it returns the current pointer where the array is) and I've been noticing something that left me a little confused comparing next() , prev() and reset() .

And I explain why.

In the PHP manual and even in Notepad++ , we see that the current() argument is a passing by reference of an element of type array , just like next() and prev() . In fact, the function declaration is this:

mixed current ( array &$array )

However, if you use it in an array returned from a function or even a temporary array (in this case, declared directly as an argument of current() ) it works normally (the argument being 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 worse is that if I pass an array directly into functions like next() and prev() , it generates 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 arguments (like in the first example)?

Note : The PHP Manual is quite emphatic: mixed current ( array &$array )

Answer:

Because there is no need to pass it by reference, the only thing this function does is return the contents of the current array pointer, and this will not modify the array, it is not necessary to pass it by reference.

Other examples of current() and other functions from the same thread:

$transport = array('foot', 'bike', 'car', 'plane');
$mode = current($transport); // $mode = 'foot';
$mode = next($transport);    // $mode = 'bike';
$mode = current($transport); // $mode = 'bike';
$mode = prev($transport);    // $mode = 'foot';
$mode = end($transport);     // $mode = 'plane';
$mode = current($transport); // $mode = 'plane';

$arr = array();
var_dump(current($arr)); // bool(false)

$arr = array(array());
var_dump(current($arr)); // array(0) { }

Note that all functions simply return and modify the pointer.

As for the error that generated when trying to apply next() in the array declaration, it is because these functions were made to work with the array pointer.

But if you try to declare the array inside the function, the pointer doesn't exist, and so it generates the error you posted in the question.

Pointer is directly linked to the creation of the array variable, it is the variable's index, which only exists from the moment you declare it.

The manual says that the array is passed by reference:

mixed current ( array &$array )

This happens because when we use functions of this "group", such as next() , it changes the pointer position, that is, it is not necessary to pass it by reference, because the function itself does this, changing the pointer position eg:

$var = array(1,2,3);
echo current($var);//1
echo next($var);//2  <- Alterou o ponteiro de $var
echo current($var);//2 
$a = $var; // <- Copiou o array
echo current($a); //2 <- Ponteiro na mesma posição de $var.

Pass by reference: http://www.php.net/manual/pt_BR/language.references.pass.php

current(): http://www.php.net/manual/pt_BR/function.current.php

Scroll to Top