PHP - Ds Set clear() Function
The PHP Ds\Set::clear() function is used to remove all elements from the current set. The elements are removed but not deleted completely, and this function empties the current set.
Syntax
Following is the syntax of the PHP Ds\Set::clear() function −
public void Ds\Set::clear( void )
Parameters
This function does not accept any parameter
Return value
This function does not return any value.
Example 1
The following program demonstrates the usage of the PHP Ds\Set::clear() function.
<?php $set = new \Ds\Set([1,2,3,4,5]); echo "The set values are: \n"; print_r($set); echo "The elements of the set after the clear function called: \n"; $set->clear(); print_r($set); ?>
Output
The above program produces the following output −
The set values are:
Ds\Set Object
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
The elements of the set after the clear function called:
Ds\Set Object
(
)
Example 2
The following is another example of the PHP Ds\Set::clear() function. We use this function to remove all the elements from this set.
<?php $set = new \Ds\Set(['a', 'e', 'I', 'o', 'u']); echo "The set values are: \n"; print_r($set); echo "\nThe elements of the set after called the clear() function: \n"; #using clear() function $set->clear(); print_r($set); ?>
Output
After executing the above program, it will display the following output −
The set values are:
Ds\Set Object
(
[0] => a
[1] => e
[2] => I
[3] => o
[4] => u
)
The elements of the set after called the clear() function:
Ds\Set Object
(
)
Example
Once theclear()function is called, we use the isEmpty() function to verify whether this function clears all the values in the set.
<?php
$set = new \Ds\Set(['a', 'e', 'I', 'o', 'u']);
echo "The set values are: \n";
print_r($set);
$set->clear();
echo "Clear() function called...\n";
$res = $set->isEmpty();
if ($res) {
echo "The current set is cleared.";
} else {
echo "Not cleared";
}
?>
Output
On executing the above program, it will generate the following output −
The set values are:
Ds\Set Object
(
[0] => a
[1] => e
[2] => I
[3] => o
[4] => u
)
Clear() function called...
The current set is cleared.