Wikis & learning

Process a queue until it is empty

A worker often repeats a step until it has no remaining work. Start with a small list so you can inspect each transition.

A worker often repeats a step until it has no remaining work. Start with a small list so you can inspect each transition.

How it works

Check the list before removing its first element. For very large in-memory queues use a queue data structure instead of repeatedly shifting an array.

PHP example

<?php
$queue = ['images', 'translations'];
while ($queue !== []) {
    echo array_shift($queue) . PHP_EOL;
}

Expected result

images followed by translations.

Try a variation

Start with an empty list. The loop should complete without printing a task.