Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

SPL iterators and structures

SplQueue is a FIFO collection. enqueue adds at the back and dequeue removes the oldest entry.

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

Syntax

$queue->enqueue($item); $item = $queue->dequeue();

SplQueue is a FIFO collection. enqueue adds at the back and dequeue removes the oldest entry. Peeking via bottom() reads the front without consuming it. Check isEmpty before removal: dequeue on an empty queue throws RuntimeException. SPL also provides iterator adapters that wrap traversal.

$q=new SplQueue();$q->enqueue("A");$q->enqueue("B");$peek=$q->bottom();$first=$q->dequeue();$second=$q->dequeue();try{$q->dequeue();}catch(RuntimeException $e){$empty=true;}echo json_encode([$peek,$first,$second,$empty]);
["A","A","B",true]

Examples

<?php
$q=new SplQueue();$q->enqueue("A");$q->enqueue("B");$peek=$q->bottom();$first=$q->dequeue();$second=$q->dequeue();try{$q->dequeue();}catch(RuntimeException $e){$empty=true;}echo json_encode([$peek,$first,$second,$empty]);

Result

["A","A","B",true]

Sources