Returns the element at the internal array pointer.
What Is current() in PHP?
Returns the element at the internal array pointer.
Its syntax is:
current(array|object $array): mixed
Parameters:
$array— Accepted as array|object; required argument.
Return type: mixed. If the pointer is beyond the last element it returns false, which may also be a valid array value.
Availability: Core PHP.
Example 1: Basic Usage
<?php
echo current(["red", "blue"]);
Output:
red
If the pointer is beyond the last element it returns false, which may also be a valid array value.
Example 2: A Different Input
<?php
$colors = ["red", "blue"]; next($colors); echo current($colors);
Output:
blue
If the pointer is beyond the last element it returns false, which may also be a valid array value.
Practical Example: Applying It in a Program
<?php
$prices = [12, 18, 24]; echo current($prices); next($prices); echo current($prices);
Output:
1218
If the pointer is beyond the last element it returns false, which may also be a valid array value.
Important Behavior and Edge Cases
- Key behavior: If the pointer is beyond the last element it returns false, which may also be a valid array value.
- Environment: Core PHP.
Comparison With Related Functions
| Function | Purpose |
|---|---|
current() | Returns the element at the internal array pointer. |
reset() | current() reads at the active array pointer; reset() moves the pointer to the beginning. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this behavior: If the pointer is beyond the last element it returns false, which may also be a valid array value | If the pointer is beyond the last element it returns false, which may also be a valid array value. |
Key Takeaways
- Returns the element at the internal array pointer.
- If the pointer is beyond the last element it returns false, which may also be a valid array value.
- Core PHP.
Summary: If the pointer is beyond the last element it returns false, which may also be a valid array value.