Open In App

PHP Ds\Queue Functions Complete Reference

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

A Queue is a linear data structure that follows a particular order in which the operations are performed. The order of queue is First In First Out (FIFO).

Requirements: PHP 7 is required for both extension and the compatibility polyfill. 

Installation: The easiest way to install data structure by using the PECL extension.

pecl install ds

Syntax:

public Ds\Queue::functionname()

Example: Below programs illustrate the Ds\Queue::clear() function in PHP:

PHP




<?php
 
// Declare new Queue
$q = new \Ds\Queue();
 
// Add elements to the Queue
$q->push("One");
$q->push("Two");
$q->push("Three");
 
echo "Initial Queue: \n";
// Display the Queue
print_r($q);
 
// clear the Queue
$q->clear();
 
echo "\nQueue after clearing:\n";
print_r($q);
 
?>


Output:

Initial Queue: 
Ds\Queue Object
(
    [0] => One
    [1] => Two
    [2] => Three
)

Queue after clearing:
Ds\Queue Object
(
)

Complete list of data structure DS\Queue:

PHP Ds\Queue Functions

Description

allocate() Allocate memory for a Queue class instance.
capacity() Check the current capacity of a Queue instance.
clear() Clear all of the elements from a Queue instance.
copy() Create a shallow copy of a particular Queue instance.
count() Get the count of elements present in a Queue instance.
isEmpty() Whether a particular Queue instance is empty or not.
peek() Get the value present at the front of a Queue.
pop() Remove and return the value present at the top of the Queue.
push() Push or insert values in a PriorityQueue instance.
toArray() Convert a Queue into an associative array in PHP.

Last Updated : 25 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads