Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - Ds Vector::count() Function



The PHPDs\Vector::count()function is used to count the number of elements present in a vector. If the vector is empty ([]), this function will return zero (0).

You can use this function with various collections such as sets, maps, stacks, etc., to retrieve the same.

Syntax

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

public Ds\Vector::count(): int

Parameters

This function does not accept any parameter.

Return value

This function returns the number of elements in a vector.

Example 1

The following is the basic example of the PHP Ds\Vector::count() function −

<?php 
   $vector = new \Ds\Vector([1, 2, 3, 4, 5]); 
   echo("The vector elements are: \n"); 
   print_r($vector);
   echo "The number of elements present in vector: ";
   print_r(count($vector));
?>

Output

The above program produces the following output −

The vector elements are:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
The number of elements present in vector: 5

Example 2

Following is another example of the PHP Ds\Vector::count() function. We use this function to count the number of elements present in this vector (["Tutorials", "Point", "Tutorix"]) −

<?php 
   $vector = new \Ds\Vector(["Tutorials", "Point", "Tutorix"]);
   echo("The vector elements are: \n"); 
   print_r($vector);
   echo "The number of elements present in vector: ";
   print_r(count($vector));
?>

Output

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

The vector elements are:
Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
    [2] => Tutorix
)
The number of elements present in vector: 3

Example 3

If the current vector is empty ([]), then the count() function will return 0

<?php 
   $vector = new \Ds\Vector([]);
   echo("The vector elements are: \n"); 
   print_r($vector);
   echo "The number of elements present in vector: ";
   print_r(count($vector));
?>

Output

Once the above program is executed, it displays the following output −

The vector elements are:
Ds\Vector Object
(
)
The number of elements present in vector: 0
php_function_reference.htm
Advertisements