Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

SplQueue

Process values in first-in, first-out order. enqueue adds at the tail and dequeue removes the head. bottom inspects the next item without consuming it.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

new SplQueue()

Process values in first-in, first-out order. enqueue adds at the tail and dequeue removes the head. bottom inspects the next item without consuming it. An empty queue cannot be dequeued, so test isEmpty when none may remain. The SplQueue::enqueue method adds a value, SplQueue::dequeue removes the oldest, SplQueue::count returns the number of values without removing any, and SplQueue::isEmpty is true exactly when the count is zero.

$q=new SplQueue(); $q->enqueue("A"); $q->enqueue("B"); $first=$q->bottom();
echo json_encode([$first,$q->dequeue(),$q->dequeue(),$q->isEmpty()]);
echo PHP_EOL,json_encode([$q->count(),$q->isEmpty()]); $q->enqueue("C"); echo PHP_EOL,json_encode([$q->count(),$q->isEmpty()]);
["A","A","B",true]
[0,true]
[1,false]

Examples

<?php
$q=new SplQueue(); $q->enqueue("A"); $q->enqueue("B"); $first=$q->bottom();
echo json_encode([$first,$q->dequeue(),$q->dequeue(),$q->isEmpty()]);
echo PHP_EOL,json_encode([$q->count(),$q->isEmpty()]); $q->enqueue("C"); echo PHP_EOL,json_encode([$q->count(),$q->isEmpty()]);

Result

["A","A","B",true]
[0,true]
[1,false]

Sources