PHP current() function

P
PHP count() function
PHP each() function

In this article, you will learn how to get the element being pointed by the array pointer. The current method returns the value of the current element in the array.

Every array has its own internal pointer, that points to the 0’th index by default. The current method is helpful if you are traversing the array and need to get the current pointer element.

What is the syntax of the current function in PHP?

current(array)
ParameterDetails
arrayThe array to get the current element – Required
PHP current function

Example of the current function

Example 1. Get the current element in the given array.

<?php
$members= array("Jawad", "Ahmad", "Sumerina");
echo current($members);
?>

Example 2. In this example, you will find all the methods related to the current method category. These methods work with the internal pointer of the arrays.

<?php
$members= array("Jawad", "Ahmad", "Sumerina", "ACS");

// Current element: Jawad
echo current($people) . "<br>";

// Next element of Jawad: Ahmad
echo next($people) . "<br>"; 

// Now the current element: Ahmad
echo current($people) . "<br>"; 

// Previous element of Ahmad: Jawad
echo prev($people) . "<br>"; 

// Last element: ACS
echo end($people) . "<br>"; 

// Previous element of ACS: Sumerina
echo prev($people) . "<br>"; 

// Current element: Sumerina
echo current($people) . "<br>"; 

// Move the internal pointer to the first element of the array:Jawad
echo reset($people) . "<br>"; 

// Next element of Jawad: Ahmad
echo next($people) . "<br>"; 

// Get key and value of Ahmad (current element) then moves the internal pointer forward
print_r (each($members));
?>
PHP count() function
PHP each() function

Add comment

Tutor Network

Share this

Learn PHP from A to Z