Reads the value at the current priority queue position.
The examples focus on the operation itself, including the important distinction: top peeks at the next item without extraction.
What It Does
Use the examples below to observe the behavior on a small dataset before extending it to application code.
Syntax and Return Value
SplPriorityQueue::current()
The displayed signature describes the API or identifies any correction or optional dependency. When a parameter is optional, supply it only when the task requires it.
Example 1: Basic Use
The queue is a max-priority queue; equal priorities have no guaranteed FIFO order. The current call is shown on a small, inspectable dataset.
<?php
$heap = new SplPriorityQueue();
$heap->insert('task-4', 4);
$heap->insert('task-12', 12);
$heap->insert('task-7', 7);
$heap->rewind();
echo json_encode($heap->current());
Output from a successful local PHP 8.4 run:
"task-12"
Example 2: Checking Behavior
The queue is a max-priority queue; equal priorities have no guaranteed FIFO order. The current call is shown on a small, inspectable dataset.
<?php
$heap = new SplPriorityQueue();
$heap->insert('task-2', 2);
$heap->insert('task-9', 9);
$heap->insert('task-5', 5);
$heap->insert('task-1', 1);
$heap->rewind();
echo json_encode($heap->current());
Output from a successful local PHP 8.4 run:
"task-9"
Example 3: Practical Example
The queue is a max-priority queue; equal priorities have no guaranteed FIFO order. The current call is shown on a small, inspectable dataset.
<?php
$heap = new SplPriorityQueue();
$heap->insert('task-19', 19);
$heap->insert('task-3', 3);
$heap->insert('task-11', 11);
$heap->rewind();
echo json_encode($heap->current());
Output from a successful local PHP 8.4 run:
"task-19"
Important Behavior and Edge Cases
Its shape depends on EXTR_DATA, EXTR_PRIORITY or EXTR_BOTH.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| SplPriorityQueue::current | Reads the value at the current priority queue position. |
| Related API or alternative | top peeks at the next item without extraction. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | Its shape depends on EXTR_DATA, EXTR_PRIORITY or EXTR_BOTH. |
| Confusing it with a related API | top peeks at the next item without extraction. |
Key Takeaways
- Reads the value at the current priority queue position.
- Its shape depends on EXTR_DATA, EXTR_PRIORITY or EXTR_BOTH.
- top peeks at the next item without extraction.
Use SplPriorityQueue::current when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.