How can i implement QUEUE in PHP?

In the circular queue, we can use an empty data cell again when we remove the cell value using a dequeue. Another important thing is we can not add value before the end of the head index( for example, if my array size is 5 and the head point to an index of 0, then we need to empty the array index 4. Which means we can add 4 value when the array size is 5). We can use this queue implementation in banking software( ex: give tokens to customers and call them according to that token). Here is the complete code below:

$devisor  = 5;
$data_array = [];
$head = 0;
$tail = 0;

function enqueue( $item = NULL ){
	global $head, $tail, $data_array, $devisor;
		echo 'invalid function call. Need to pass item as a function parametter';
	$tail = $tail % $devisor;
	$next_tindex = $tail + 1;
	if( $head == $next_tindex % $devisor){
		echo 'Queue is full';
		$data_array[$tail] = $item;
	return $data_array;

function dequeue(){
	global $head,$tail,$devisor,$data_array;
	if($tail == $head){
		echo 'Queue is empty';
		return -1;
	$item = $data_array[$head];
	$head = ++$head % $devisor;
	return $item;

echo '<pre>';
echo '</pre>';
echo '<pre>';
echo '</pre>';

Leave a Reply

Your email address will not be published. Required fields are marked *