Understanding PHP's current() Function: Retrieving the Current Array Element

The article explains PHP's current() function, describing its purpose of returning the value of the array element pointed to by the internal pointer without moving it, detailing its parameters, return values, and providing practical code examples demonstrating usage with various pointer operations.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Understanding PHP's current() Function: Retrieving the Current Array Element

PHP's current() function returns the value of the array element that the internal pointer currently points to, without moving the pointer.

Each array has an internal pointer that initially points to the first inserted element. The function takes a reference to an array as its sole parameter.

If the internal pointer is beyond the end of the array, current() returns FALSE.

Example usage:

<?php
$transport = array('foot','bike','car','plane');
$mode = current($transport); // returns 'foot'
$mode = next($transport);    // moves pointer, returns 'bike'
$mode = current($transport); // returns 'bike'
$mode = prev($transport);    // moves back, returns 'foot'
$mode = end($transport);     // moves to last element, returns 'plane'
$mode = current($transport); // returns 'plane'

$arr = array();
var_dump(current($arr)); // bool(false)

$arr = array(array());
var_dump(current($arr)); // array(0) { }
?>
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

array functionscurrent
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.