PHP SplQueue::isEmpty
A queue can be checked for waiting tickets before the receptionist dequeues one.
A simple example
SplQueue::isEmpty
- Start with Queue containing no items
- Do this Request the first item
- Result No item available
Syntax
$queue->isEmpty()
Run these examples in PHP
Save one example at a time as lesson.php. Use PHP 8.2 or later.
php lesson.phpCall the next person at the clinic
A receptionist calls patients from the morning queue. If the final patient has already checked in, the screen should display an empty-queue message instead of trying to issue another number.
<?php
$patients = new SplQueue();
echo $patients->isEmpty() ? 'No patients waiting' : $patients->dequeue();
No patients waiting
The queue has no patient at its front. The screen reports the empty state to the receptionist.
Call a waiting pupil
The music desk has one pupil in its equipment queue. Take the next name from the queue. An empty queue should send the desk back to waiting.
Check an empty ticket queue
The counter opens before anyone has taken a ticket. Take the next name from the queue. An empty queue should send the desk back to waiting.
What to watch for
dequeue on an empty SplQueue throws a RuntimeException.