What is the Ds\Set::isEmpty method in PHP?
The isEmpty method of the Set data structure can be used to check if a Set object is empty.
A Set is a collection of unique values. In Ds\Set we can store any type of value including objects. The insertion order of the elements are preserved.
Installing DS
In PHP, the data structures are not installed by default. We can install them by following these instructions on php.net.
Syntax
public Ds\Set::isEmpty(): bool
This method returns true if the Set object is empty. Otherwise, it returns false.
Example
<?php$set1 = new \Ds\Set([10, 20, 30]);$set2 = new \Ds\Set();var_dump($set1->isEmpty());var_dump($set2->isEmpty());?>
Output
bool(false)
bool(true)
Explanation
In the code above,
-
We have created two objects of type
Setdata structure. -
The first set object (
set1) is created with some elements, whereas the second set object (set2) is created without any elements in it. -
Then we called the
isEmptymethod onset1andset2object. Forset1,isEmptymethod returnsfalsebecauseset1has some elements. Forset2,isEmptymethod returnstruebecauseset2has no elements.