Updated April 4, 2023
Introduction to PHP queue
Queue in PHP is a data structure that operates based on First In First Out which is also called FIFO, and four basic operations define a queue, namely init, enqueue, dequeue and isEmpty, where init operation is used for the creation of the queue and enqueue operation is used to add an item at the end of the queue, or the tail of the queue and dequeue operation is used to remove an item from the front of the queue or the head of the queue and isEmpty operation is used to check if the queue is empty or not that is it returns if the queue contains no more items or not.
The syntax to declare queue in PHP is as follows:
enqueue(item_to_added_to_the_queue);
dequeue();
where item_to_be_added_to_the_queue is the item that is going to be added to the queue at the end of the queue or the tail of the queue.
Working of the queue in PHP
- Queue in PHP is a data structure that operates based on First In First Out, which is also called FIFO.
- Four basic operations define a queue, namely init, enqueue, dequeue, and isEmpty.
- init operation is used for the creation of the queue.
- enqueue operation is used to add an item at the end of the queue or the tail of the queue.
- The dequeue operation is used to remove an item from the front of the queue or the head of the queue.
- isEmpty operation is used to check if the queue is empty or not; that is, it returns if the queue contains no more items or not.
Examples of PHP queue
Here are the following examples mention below
Example #1
PHP program to add the items to a queue from the end of the queue using enqueue() function and remove the items from the front of the queue using the dequeue() function, and display the contents of the queue:
Code:
<?php
//creating an instance of SplQueue class
$newqueue = new SplQueue();
//using enqueue() function to the add items to the queue from the tail of the queue
$newqueue->enqueue('Welcome');
$newqueue->enqueue('to');
$newqueue->enqueue('PHP');
//using rewind() function to bring the file pointer to the beginning of the queue
$newqueue->rewind();
//using valid() function to check if the queue is valid or not after using rewind() function and then displaying the elements of the queue
while($newqueue->valid()){
echo $newqueue->current(),"\n";
$newqueue->next();
}
//printing the contents of the queue in a human readable format by using print_r function
print_r ($newqueue);
//Removing the first two items from the head of the queue using dequeue() function and then displaying the contents of the queue in human readable form using print_r function
$newqueue->dequeue();
$newqueue->dequeue();
print_r ($newqueue);
?>
Output:
In the above program, we are creating an instance of the SplQueue() class. Then we are adding items to the queue from the tail of the queue or the end of the queue. Then we are making use of the rewind() function to bring the file pointer to the beginning of the queue. Then we are using the valid() function to check if the queue is valid or not after using the rewind() function and then displaying the elements of the queue. Then we are printing the contents of the queue in a human-readable format by using the print_r function. Then we remove the first two items from the head of the queue using the dequeue() function and then display the queue contents after using the dequeuer() function in human-readable form using print_r function. The output is shown in the snapshot above.
Example #2
PHP program to add the items to a queue from the end of the queue using enqueue() function and remove the items from the front of the queue using the dequeue() function, and display the contents of the queue:
Code:
<?php
//creating an instance of SplQueue class
$newqueue = new SplQueue();
//using enqueue() function to the add items to the queue from the tail of the queue
$newqueue->enqueue('Welcome');
$newqueue->enqueue('to');
$newqueue->enqueue('EDUCBA');
//using rewind() function to bring the file pointer to the beginning of the queue
$newqueue->rewind();
//using valid() function to check if the queue is valid or not after using rewind() function and then displaying the elements of the queue
while($newqueue->valid()){
echo $newqueue->current(),"\n";
$newqueue->next();
}
//printing the contents of the queue in a human readable format by using print_r function
print_r ($newqueue);
//Removing the first two items from the head of the queue using dequeue() function and then displaying the contents of the queue in human readable form using print_r function
$newqueue->dequeue();
$newqueue->dequeue();
$newqueue->dequeue();
print_r ($newqueue);
?>
Output:
In the above program, we are creating an instance of the SplQueue() class. Then we are adding items to the queue from the tail of the queue or the end of the queue. Then we are making use of the rewind() function to bring the file pointer to the beginning of the queue.
Then we are using the valid() function to check if the queue is valid or not after using the rewind() function and then displaying the elements of the queue. Then we are printing the contents of the queue in a human-readable format by using the print_r function. Then we remove all the three items from the head of the queue using the dequeue() function and then display the queue contents after using the dequeuer() function in human-readable form using print_r function, which is an empty queue. The output is shown in the snapshot above.
Recommended Articles
This is a guide to the PHP queue. Here we discuss the introduction, syntax, and working of queue in PHP along with different examples and their outputs. You may also have a look at the following articles to learn more –