PHP Array current() Function (With Examples)

In this tutorial, we will learn about the PHP current() function with its usage, syntax, parameters, return value, and examples. By IncludeHelp Last updated : December 31, 2023

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

The syntax of the current() function:

current(arr);

Parameters

The parameters of the current() function:

  • arr: An input array.

Return Value

The return type of this method is mixed, it returns the value of the array element that's currently being pointed to by the internal pointer. [Source]

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.

Sample Input/Output

Input:
$arr1 = array(10, 20, 30, 40, 50);
Output:
10

Input:
$arr2 = array("name" => "Amit", "age" => 21, "Gender" => "Male");
Output:
Amit

PHP current() Function Example

<?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

The output of the above example is:

10
Amit

To understand the above example, you should have the basic knowledge of the following PHP topics:

All PHP Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.