Home »
PHP
PHP current() function with example
PHP current() function: Here, we are going to learn about the current() function with example in PHP.
Submitted by IncludeHelp, on February 26, 2019
PHP current() function
An array in PHP has a current pointer which points to the first element by default, current() function is used to get the current elements of an array. It accepts an array and returns its current element.
Syntax:
current(array);
Note:
- In an array which contains keys and values, it returns only value not a key.
- current() returns only the current element, it does not move the pointer to next element.
Examples:
Input:
$arr1 = array(10, 20, 30, 40, 50);
Output:
10
Input:
$arr2 = array("name" => "Amit", "age" => 21, "Gender" => "Male");
Output:
Amit
PHP code:
<?php
$arr1 = array(10, 20, 30, 40, 50);
$arr2 = array("name" => "Amit", "age" => 21, "Gender" => "Male");
//printing current element
echo current($arr1). "\n";
echo current($arr2). "\n";
?>
Output
10
Amit
TOP Interview Coding Problems/Challenges