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

PHP Ds Vector::isEmpty() Function



The PHP Ds\Vector::isEmpty() function is used to determine whether the current vector is empty. This function returns a boolean value 'true' if the vector is empty ([]), otherwise it returns 'false'.

The Ds\Vector provides another function named count(), which returns the size of the vector. If you call this function on an "empty" vector, it returns zero (0).

Syntax

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

public Ds\Vector::isEmpty(): bool

Parameters

This function does not accept any parameter.

Return value

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

Example 1

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

<?php 
   $vector = new \Ds\Vector([1, 2, 3, 4, 5]);
   echo "The vector elements are: \n";
   print_r($vector);
   echo "Is the vector is empty? ";
   var_dump($vector->isEmpty());
?>

Output

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

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

Example 2

If the vector is empty, this function will return 'true'.

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

<?php 
   $vector = new \Ds\Vector([]);
   echo "The vector elements are: \n";
   print_r($vector);
   echo "Is the vector is empty? ";
   var_dump($vector->isEmpty());
   echo "The number of elements in vector: ";
   print_r($vector->count());
?>

Output

The above program generates the following output −

The vector elements are:
Ds\Vector Object
(
)
Is the vector is empty? bool(true)
The number of elements in vector: 0

Example 3

Using the isEmpty() function result within the conditional statement to determine the vector is empty −

<?php 
   $vector = new \Ds\Vector(['a', 'e', 'i', 'o', 'u']);
   echo "The vector elements are: \n";
   print_r($vector);
   echo "Is the vector is empty? ";
   $res = $vector->isEmpty();
   if($res){
	   echo "Empty";
   }
   else{
	   echo "Not empty";
   }
?>

Output

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

The vector elements are:
Ds\Vector Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
Is the vector is empty? Not empty
php_function_reference.htm
Advertisements