Create a Simple Queue System in PHP
Create a Simple Queue System in PHP

Create a Simple Queue System in PHP

Create a Simple Queue System in PHP

Hello friends, in todays article, we will create a fun little project. We will Create a Simple Queue System in PHP just to give you the idea how a Queue System works. So lets get started.

First of all, what is a queue system? We all know that, a queue is a simple line for any service. In real life, let’s say you aree in a line for coffee. The barista serves one by one “First Come, First Serve” basis. In programming world we do the same for various purposes. Like you want to send mail to 1000 customers. So you can prepare a queue system to send mail one by one. It can be run in background as a separate job.

To implement a basic queue in PHP, we can use an array and utilize array functions to add and remove elements. Here’s an example implementation:

class Queue

{

    protected $queue;

    public function __construct()
    {

        $this->queue = [];

    }

    public function enqueue($item)
    {

        array_push($this->queue, $item);

    }

    public function dequeue()
    {

        if ($this->isEmpty()) {

            return null;
        }

    return array_shift($this->queue);
    }

    public function isEmpty()
    {

        return empty($this->queue);

    }

    public function size()
    {

        return count($this->queue);

    }

}

This example defines a `Queue` class with the following methods:

– `enqueue($item)`: this  method adds an item to the end of the queue.
– `dequeue()`: this method removes and returns the item from the front of the queue.
– `isEmpty()`: checks if the queue is empty.
– `size()`: this method returns the size of the queue.

Below is the implementation or usage of the  `Queue` class:

$queue = new Queue();
$queue->enqueue('Item 1');
$queue->enqueue('Item 2');
$queue->enqueue('Item 3');

echo $queue->dequeue(); // Output: Item 1
echo $queue->dequeue(); // Output: Item 2

echo $queue->size(); // Output: 1
echo $queue->isEmpty() ? 'Queue is empty' : 'Queue is not empty'; 
// Output: Queue is not empty

For more clarification, This demonstrates how to add items to the queue using `enqueue()` and remove items from the front of the queue using `dequeue()`. It also shows how to check the size of the queue using `size()` and whether the queue is empty using `isEmpty()`.

Keep in mind that this is a simple implementation of a queue using an array. For more complex scenarios or performance optimizations, you might consider using other data structures or exploring built-in PHP queue implementations like `SplQueue`.

 

That’s all for today. Home you liked this little article to give you a simple idea regarding  “Queue“. See you in some other day!

 

Check Out More Resources:

Articles:
Website: https://laravelplug.com/
YouTube Channel: https://www.youtube.com/channel/UCnTxncHQcBSNs4RCuhWgF-A?sub_confirmation=1
WordPress Playlist: https://www.youtube.com/watch?v=8VvXzFAFwQU&list=PLVoVZqAh8dTLHZ51egvEU7dsOrRCSIMWx
Tools Playlist: https://www.youtube.com/watch?v=gTQoUn3BQkM&list=PLVoVZqAh8dTK-DWHBGWOEEvzvPQrgFne-

WordPress Tutorials: https://laravelplug.com/category/wordpress/
Laravel Tutorials: https://laravelplug.com/category/laravel/
PHP Tutorials: https://laravelplug.com/category/php/
SQL Tutorials: https://laravelplug.com/category/sql/
Various Tips: https://laravelplug.com/category/tips/
Useful Tools: https://laravelplug.com/category/tools/

Socials:

Twitter: https://twitter.com/LaravelPlug
Pinterest: https://www.pinterest.com/LaravelPlugCom/
Facebook: https://www.facebook.com/groups/1020759861842360
Mail: info@laravelplug.com

#php #guide #queue

Editorial Staff

A Learner and trying to share what I learned!