Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
ip
array
communitycreator

What is the current() method in PHP?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The current() method is used to get the value of a current element pointed at by the array’s internal pointer (IP).

In PHP, each array contains an internal pointer that points to the current element. It initially points to the first element of the array. We can move the pointer positions using methods like next, prev, etc.

Syntax


current(array|object $array): mixed

Return value

The return is false if an array is empty or if the position pointed to by the internal pointer is beyond the array’s length.

Code

<?php
$numbers = [1,2,3];
// 1
echo "Current Value is : ". current($numbers). "\n";
//2
next($numbers);
echo "Current Value is : ". current($numbers). "\n";
//3
next($numbers);
echo "Current Value is : ". current($numbers). "\n";
// beyond length so false
next($numbers);
echo "Current Value is : ";
var_dump(current($numbers));
?>

Explanation

In the code above:

  • We created a numbers array.

  • We used the current method to print the value pointed by the internal pointer of the array.

  • We used the next method to move the pointer position one step forward and printed the value pointed by the pointer.


The next method moves the pointer position one step forward. We can use the prev method to move back.

RELATED TAGS

php
ip
array
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring