PHP - Ds Vector::clear() Function



The PHP Ds\Vector::clear() function is used to remove all values of a vector. Once this function is called, the vector will be empty ([]), having no elements.

You can verify whether the vector is empty using the isEmpty() function. If the current vector is empty, this function returns true.

Syntax

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

public void Ds\Vector::clear( void )

Parameters

This function does not accept any parameter.

Return value

This function doesn't return any value.

Example 1

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

<?php 
   $vector = new \Ds\Vector([1, 2, 3, 4, 5]); 
   echo("The original vector: \n"); 
   print_r($vector);
   echo "The vector elements after clear() function called: \n";
   $vector->clear();
   print_r($vector);
?>

Output

The above program generates the following output −

The original vector:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
The vector elements after clear() function called:
Ds\Vector Object
(
)

Example 2

Following is another example of the PHP Ds\Vector::clear() function. We use this function to remove all the values of the given vector −

<?php 
   $vector = new \Ds\Vector(["Tutorials", "Point", "Tutorix"]);
   echo("The original vector: \n"); 
   print_r($vector);
   echo("The vector after clear() function called: \n");   
   $vector->clear();
   print_r($vector);
   echo "The number of elements in vector: ";
   print_r($vector->count());   
?> 

Output

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

The original vector:
Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
    [2] => Tutorix
)
The vector after clear() function called:
Ds\Vector Object
(
)
The number of elements in vector: 0
php_function_reference.htm
Advertisements