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 Set data 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 isEmpty method on set1 and set2 object. For set1, isEmpty method returns false because set1 has some elements. For set2, isEmpty method returns true because set2 has no elements.

Free Resources