CopyPastehas never been so tasty!

Queue handling in PHP

by anonymous

  • 0
  • 0
  • 0


 Queue is FIFO  data structure. First In First Out as incase of persons in a row the first person in a row will be served first.  There are two functions that are required in a queue. One is insert data in a queue and second is remove data from the queue


There are only two functions which are required with a queue. One will insert data into the queue and other will remove data from the queue. (One function is use to grow the queue and other is used to shrink the queue) We call these two functions Enqueue and Dequeue.


Ø  Enqueue is a method for adding new items to the queue. It will add the new items from the back of the queue.

Ø  Dequeue is a method for removing items from the queue. It will remove items from the front of the queue.






function &queue_initialized() {

$new_array = array();

return $new_array;


function queue_destroyed(&$queue) {



function queue_enqueue(&$queue, $value) {

$queue[] = $value;


function queue_dequeue(&$queue) {

return array_shift($queue);


function queue_peek_value(&$queue) {

return $queue[0];


function queue_size(&$queue) {

return count($queue);


function queue_rotate(&$queue) {

$queue[] = array_shift($queue);


$fisrqueue =& queue_initialized();

queue_enqueue($fisrqueue, 'Tahreem');

queue_enqueue($fisrqueue, 'Rizwan');

queue_enqueue($fisrqueue, 'Farhan');

queue_enqueue($fisrqueue, 'Misbah');


echo "The Queue size=". queue_size($fisrqueue)."<br/>";


echo "Front value of the queue is=". queue_peek_value($fisrqueue)."<br/>";


echo "Queue Rotate".queue_rotate($fisrqueue)."<br/>";


echo "Removed the values at the front of the queue:". queue_dequeue($fisrqueue);









The Queue size=4

Front value of the queue is=Tahreem

Queue Rotate

Removed the values at the front of the queue:Rizwan






In the above code we have initialized an array $fisrqueue. The & queue_initialized() the array in $fisrqueue. Then we have insert values into array that is type of queue first value is inserted at first index then second value and so on with the help of method queue_enqueue. That adds values to the back of the queue. Then we have count the size of queue by passing it to queue_size function. The queue_size function counts the element of an array by passing it to count function of php. Then queue_peek_value is returned the first value of the queue. Queue_rotate function rotates the array by deleting the first element. Then we have removed elements by calling dequeue function. At the end we have destroyed the queue by calling the function queue_destroyed that unset the entire queue.

Add A Comment: