PHP - Ds Map::isEmpty() Function



The PHP Ds\Map::isEmpty() function is used to determine whether the map is empty. This function returns a boolean value 'true' if the current map is empty; otherwise, it returns 'false'. If you call the count() function on the map, it will return '0' if the map is empty.

Syntax

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

public Ds\Map::isEmpty(): bool

Parameters

This function does not accept any parameter.

Return value

This function returns 'true' if the map is empty, otherwise 'false'.

Example 1

The following program demonstrates the usage of the PHP Ds\Map::isEmpty() function −

<?php 
   $map = new \Ds\Map();
   echo "The map elements are: \n";
   print_r($map);
   echo "Is map is empty? ";
   #using isEmpty() function
   var_dump($map->isEmpty()); 
?>

Output

The above program displays the following output −

The map elements are:
Ds\Map Object
(
)
Is map is empty? bool(true)

Example 2

If the current map is not empty, the isEmpty() function returns 'false'.

Following is another example of the PHP Ds\Map::isEmpty() function. We use this function to check whether this (["Tutorials", "Point", "India"]) map is empty or not −

<?php 
   $map = new \Ds\Map(["Tutorials", "Point", "India"]);
   echo "The map elements are: \n";
   foreach($map as $key=>$value){
	   echo "[".$key."] = ".$value."\n";
   }
   echo "Is map is empty? ";
   #using isEmpty() function
   var_dump($map->isEmpty()); 
?>

Output

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

The map elements are:
[0] = Tutorials
[1] = Point
[2] = India
Is map is empty? bool(false)

Example 3

Using the isEmpty() function result within the conditional statement to determine whether the given map is empty or not −

<?php 
   $map = new \Ds\Map(['a', 'e', 'i', 'o', 'u']);
   echo "The map elements are: \n";
   foreach($map as $key=>$value){
	   echo "[".$key."] = ".$value."\n";
   }
   echo "Is map is empty? ";
   #using isEmpty() function
   $result = $map->isEmpty();
   if($result){
	   echo "Empty.";
   }
   else{
	   echo "Not empty";
   }	   
?>

Output

Once the above program is executed, the following output will be displayed:

The map elements are:
[0] = a
[1] = e
[2] = i
[3] = o
[4] = u
Is map is empty? Not empty
php_function_reference.htm
Advertisements