PHP current() Function (original) (raw)

Last Updated : 20 Jun, 2023

The current() function is an inbuilt function in PHP.

Syntax:

current($array)

Parameters: The current() function accepts a single parameter $array. It is the array of which we want to find the current element.

Return Values: It returns the value of the element in the array which the internal pointer is currently pointing to. If the array is empty then the current() function returns FALSE.

Examples:

Input : current(array("John", "b", "c", "d")) Output : John Explanation : Here as we see that input array contains many elements and the output is "John" because first element is John and current() function returns the element to which internal pointer is currently pointing.

Input: current(array("abc", "123", "7")) Output: abc

Below programs illustrate the current() function in PHP:

Program 1:

PHP

<?php

$arr = array ( "Ram" , "Shita" , "Geeta" );

echo current( $arr );

?>

Output:

Ram

Program 2:

PHP

<?php

$arr = array ( 'Sham' , 'Mac' , 'Jhon' , 'Adwin' );

echo current( $arr ). "\n" ;

echo next( $arr ). "\n" ;

echo current( $arr ). "\n" ;

echo next( $arr ). "\n" ;

echo next( $arr ). "\n" ;

echo current( $arr ). "\n" ;

?>

Output:

Sham Mac Mac Jhon Adwin Adwin

Note: The current() function returns False when array is empty i.e, do not contain any elements, and also it returns false when internal pointer go out of the bound i.e beyond the end of the last element.

Reference:
http://php.net/manual/en/function.current.php