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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.