PHP | current() Function
The current() function is an inbuilt function in PHP.
- It is used to return the value of the element in an array which the internal pointer is currently pointing to.
- The current() function does not increment or decrement the internal pointer after returning the value.
- In PHP, all arrays have an internal pointer. This internal pointer points to some element in that array which is called as the current element of the array.
- Usually, the current element is the first inserted element in the array.
Syntax:
current($rray)
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 ouput 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 pos() function in PHP:
Program 1:
<?php // input array $arr = array("Ram", "Shita", "Geeta"); // Here current function returns the // first element of the array. echo current($a); ?> |
Output:
Ram
Program 2:
<?php $arr = array('Sham', 'Mac', 'Jhon', 'Adwin'); // Here current element is Sham. echo current($arr)."\n"; // increment internal pointer to point // to next element i.e, Mac echo next($arr)."\n"; // printing the current element as // for now current element is Mac. echo current($arr)."\n"; // increment internal pointer to point // to next element i.e, Jhon. echo next($arr)."\n"; // increment internal pointer to point // to next element i.e, Adwin. echo next($arr)."\n"; // printing the current element as for // now current element is Adwin. 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 return 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
Recommended Posts:
- How to get current function name in PHP?
- PHP | SplFixedArray current() Function
- PHP | SimpleXMLIterator current() Function
- PHP | Imagick current() Function
- PHP | SplObjectStorage current() Function
- PHP | SplHeap current() Function
- PHP | SplDoublyLinkedList current() Function
- PHP | SplFileObject current( ) Function
- Get the current URL using jQuery?
- How to get current year in PHP ?
- How to get the current weeknumber of the year?
- How to get the current date in JavaScript ?
- How to get the current date and time in seconds?
- How to get the first and last date of current month using JavaScript ?
- Print current day and time using HTML and JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



