PHP - Ds Deque::clear() Function
The PHP Ds\Deque::clear() function is used to remove all the values from a deque. If the current deque is already empty, no changes will be reflected.
This function clears all the elements, no matter what type of elements the deque contains. When invoked, the deque will become empty, which you can verify using the isEmpty() function, which returns 'true' if the deque is empty or 'false' otherwise.
Syntax
Following is the syntax of the PHP Ds\Deque::clear() function −
public void Ds\Deque::clear( void )
Parameters
This function does not accept any parameter.
Return value
This function does not return any value, instead it clears the current deque.
Example 1
The following program demonstrates the usage of the PHP Ds\Deque::clear() function −
<?php $deque = new \Ds\Deque([10, 20, 30, 40]); echo "The deque elements are: \n"; print_r($deque); #using the clear() function $deque->clear(); echo "The deque after clear: \n"; print_r($deque); ?>
Output
The above program displays the following output −
The deque elements are:
Ds\Deque Object
(
[0] => 10
[1] => 20
[2] => 30
[3] => 40
)
The deque after clear:
Ds\Deque Object
(
)
Example 2
Following is another example of the PHP Ds\Deque::clear() function. We use this function to remove all the elements from this deque (["Tutorials", "Point", "India"]) −
<?php $deque = new \Ds\Deque(["Tutorials", "Point", "India"]); echo "The deque elements are: \n"; print_r($deque); #using the clear() function $deque->clear(); echo "The deque after clear: \n"; print_r($deque); echo "Is deque is empty? "; var_dump($deque->isEmpty()); ?>
Output
After executing the above program, it will display the following output −
The deque elements are:
Ds\Deque Object
(
[0] => Tutorials
[1] => Point
[2] => India
)
The deque after clear:
Ds\Deque Object
(
)
Is deque is empty? bool(true)