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

PHP - Ds\Collection::isEmpty() Function



The PHP Ds\Collection::isEmpty() function is used to determine whether the current collection is empty or not.

You can directly call this function on various collections such as a set, stack, vector, dequeue, etc. This function returns a boolean value 'true' if the current collection is empty and 'false' if it is not.

Syntax

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

abstract public bool Ds\Collection::isEmpty( void )

Parameters

This function does not accept any parameter.

Return value

This function returns 'true' if the current collection is empty, otherwise, it returns 'false'.

Example 1

If the current collection is empty, this function returns 'true'.

Following is a basic example of the PHP Ds\Collection::isEmpty() function −

<?php 
   $collection = new \Ds\Vector(); 
   echo "Vector elements: \n";
   print_r($collection);
   #using isEmpty() function
   $res = $collection->isEmpty();
   print_r("Is this collection is empty? ");
   var_dump($res);
?>

Output

The above program returns 'true' as shown in the output below −

Vector elements:
Ds\Vector Object
(
)
Is this collection is empty? bool(true)

Example 2

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

Following is another example of the PHP Ds\Collection::isEmpty() function. We use this function to check whether this vector ([1, 2, 3, 4, 5]) (collection) is empty −

<?php 
   $collection = new \Ds\Vector([1, 2, 3, 4, 5]); 
   echo "Vector elements: \n";
   print_r($collection);
   #using isEmpty() function
   $res = $collection->isEmpty();
   print_r("Is this collection is empty? ");
   var_dump($res);
?>

Output

After executing the above program, it will return 'false' as shown in the below output −

Vector elements:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
Is this collection is empty? bool(false)

Example 3

In the example below of PHP Ds\Collection::isEmpty() function, we create a new empty vector (collection) and invoke the isEmpty() function on it before and after adding elements to see the results −

<?php 
   $vect = new \Ds\Vector();
   echo "Before adding elements: \n";
   $res = $vect->isEmpty();
   echo "Is this collection is empty? ";
   var_dump($res);
   #adding elements
   $vect->push(121);
   $vect->push(272);
   $vect->push(31);
   $vect->push(26);
   $vect->push(99);
   $vect->push(81);
   echo "Vector elements are: \n";
   print_r($vect);
   $res = $vect->isEmpty();
   echo "After adding element to this collection: \n";
   echo "Is this collection is empty? ";
   var_dump($res);
?>

Output

Once the above program is executed, it will display the following output −

Before adding elements:
Is this collection is empty? bool(true)
Vector elements are:
Ds\Vector Object
(
    [0] => 121
    [1] => 272
    [2] => 31
    [3] => 26
    [4] => 99
    [5] => 81
)
After adding element to this collection:
Is this collection is empty? bool(false)

Example 4

Let's use the isEmpty() function result in the conditional statement to check whether the current collection is empty or non-empty −

<?php 
   $seq = new \Ds\Vector(["test_string", 1525, false]);
   echo "Vector elements are: \n";
   var_dump($seq);
   $res = $seq->isEmpty();
   if($res){
      print("Collection is empty");	   
   }else{
      print("Collection is not empty");	   
   }   
?>

Output

On executing the above program, it will generate the following output −

Vector elements are:
object(Ds\Vector)#1 (3) {
  [0]=>
  string(11) "test_string"
  [1]=>
  int(1525)
  [2]=>
  bool(false)
}
Collection is not empty
php_function_reference.htm
Advertisements