Friday, October 17, 2025
HomeLanguagesPHP DsQueue pop() Function

PHP Ds\Queue pop() Function

The Ds\Queue::pop() Function in PHP is used to remove and return the value present at the top of the Queue. In other words, it returns the value present at the front of the Queue and also removes it from the Queue.

Syntax: 

mixed public Ds\Queue::pop ( void )

Parameters: This function does not accepts any parameters.

Return Value: This function returns the value with present at the top of the Queue. The return type of the function is mixed and depends on the type of value stored in the Queue.

Exception: This function throws an Underflow Exception if the Queue is empty.

Below programs illustrate the Ds\Queue::pop() Function in PHP:

Program 1:  

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 is: \n";
print_r($q);
 
// Pop an element
echo "\nPopped element is: ";
print_r($q->pop());
 
echo "\n\nFinal Queue is: \n";
print_r($q);
 
?>


Output: 

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

Popped element is: One

Final Queue is: 
Ds\Queue Object
(
    [0] => Two
    [1] => Three
)





 

Program 2: 

PHP




<?php
 
// Declare new Queue
$q = new \Ds\Queue();
 
// Add elements to the Queue
$q->push("Geeks");
$q->push("for");
$q->push("Geeks");
 
echo "Initial Queue is: \n";
print_r($q);
 
// Pop an element
echo "\nPopped element is: ";
print_r($q->pop());
 
echo "\n\nFinal Queue is: \n";
print_r($q);
 
?>


Output: 

Initial Queue is: 
Ds\Queue Object
(
    [0] => Geeks
    [1] => for
    [2] => Geeks
)

Popped element is: Geeks

Final Queue is: 
Ds\Queue Object
(
    [0] => for
    [1] => Geeks
)





 

Reference: http://php.net/manual/en/ds-queue.pop.php
 

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS