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

PHP - Ds\Queue::isEmpty() Function



The PHP Ds\Queue::isEmpty() function is used to determine whether the current queue is empty. An empty queue means it has no elements.

This function returns a boolean value: it returns true if the queue is empty ([]), and false otherwise. You can use the clear() function to empty the queue.

Syntax

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

public Ds\Queue::isEmpty(): bool

Parameters

This function does not accept any parameter.

Return value

This function returns "true" if the queue is empty, or "false" otherwise.

Example 1

If the current queue is empty ([]), the PHP Ds\Queue::isEmpty() function will return "true" −

<?php  
   $queue = new \Ds\Queue([]);
   echo "The queue elements are: \n";
   print_r($queue);
   echo "Does the queue is empty? ";
   var_dump($queue->isEmpty());   
?>

Output

The above program returns "true" −

The queue elements are:
Ds\Queue Object
(
)
Does the queue is empty? bool(true)

Example 2

If the current queue is non-empty, this function returns false.

Following is another example of the PHP Ds\Queue::isEmpty() function. We use this function to check whether this queue is empty ([]) −

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

Output

After executing the above program, it returns 'false' −

The queue elements are:
Ds\Queue Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
Does the queue is empty? bool(false)

Example 3

Using the isEmpty() function result within the "conditional" statement to check whether the queue is empty ([]) −

<?php  
   $queue = new \Ds\Queue([10, 20, 30, 40, 50]);
   echo "The queue elements are: \n";
   print_r($queue);
   echo "Does the queue is empty(initial)? ";
   var_dump($queue->isEmpty());   
   echo "Invoking clear function...";
   $queue->clear();
   $result = $queue->isEmpty();
   if($result){
	   echo "\nThe queue is empty after clear.";
   }
   else{
	   echo "\nNot empty";
   }
?>

Output

Following is the output of the above program −

The queue elements are:
Ds\Queue Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)
Does the queue is empty(initial)? bool(false)
Invoking clear function...
The queue is empty after clear.
php_function_reference.htm
Advertisements