Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - Ds\Queue::clear() Function



The PHP Ds\Queue::clear() function is used to remove all values from a queue. Once this function is invoked, the queue will be empty ([]).

The Ds\Queue class also provides the isEmpty() function, which returns true if the queue is empty and false if it contains elements.

Syntax

Following is the syntax of the PHP Ds\Queue::clear() function −

public Ds\Queue::clear(): void

Parameter

This function does not accept any parameters.

Return value

This function does not return any value.

Example 1

The following is the basic example of the PHP Ds\Queue::clear() function −

<?php  
   $queue = new \Ds\Queue([10, 20, 30, 40, 50]);
   echo "The queue elements are: \n";
   print_r($queue);
   echo "The queue after clear: \n";
   #using clear() function
   $queue->clear();
   print_r($queue);
?>

Output

The above program produces the following output −

The queue elements are:
Ds\Queue Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)
The queue after clear:
Ds\Queue Object
(
)

Example 2

The following is another example of the PHP Ds\Queue::clear() function. We use this function to remove all the elements from this queue (["Tutorials", "Point", "India"]) −

<?php  
   $queue = new \Ds\Queue(["Tutorials", "Point", "India"]);
   echo "The queue before clear: \n";
   print_r($queue);
   echo "The queue after clear: \n";
   #using clear() function
   $queue->clear();
   print_r($queue);
   echo "Does the queue is empty? ";
   var_dump($queue->isEmpty());
?>

Output

After executing the above program, the following output will be displayed −

The queue before clear:
Ds\Queue Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
The queue after clear:
Ds\Queue Object
(
)
Does the queue is empty? bool(true)
php_function_reference.htm
Advertisements