PHP current function
PHP current returns an element in an array, more specifically its value.
All arrays have an internal pointer, which is initialized to the first element in the array. Therefore, current will have the value of the first element if next is not used.
Originally, current was created to be used on objects as well, but this is not advisable currently as this feature is deprecated.
Description / Descripcióncurrent(array|object $array): mixedSintaxis / Sintax
$value = current($array);
Parametro
$array – Array from which we will take its current value.
Retorno
mixed (can be any value) – the value of the current element
false – if the pointer is out of range
Ejemplos de PHP current
First, let’s see a simple example: in this, we will understand what current returns.
php current$calzados = array( "tenis" => "Nike", "sandalias" => "Adidas", "botas" => "Timberland", "zapatos" => "Clarks" ); echo current($calzados);
Nike
Let’s see another example with the same array:
utilizando current como condición en un loop$calzados = array( "tenis" => "Nike", "sandalias" => "Adidas", "botas" => "Timberland", "zapatos" => "Clarks" ); // Establecer el puntero interno del array al primer elemento reset($calzados); // Recorrer el array while ($marca = current($calzados)) { $tipo = key($calzados); echo "Tipo: $tipo, Marca: $marca\n"; next($calzados); }
Tipo: tenis, Marca: Nike Tipo: sandalias, Marca: Adidas Tipo: botas, Marca: Timberland Tipo: zapatos, Marca: Clarks
The recommendation for iterating through an array is to use the PHP foreach statement. If you are still interested in using while, you may want to read about the PHP next() function.