• PHP Video Tutorials

PHP - Ds Set count() Function



The PHP Ds\Set::count() function is used to retrieve the number of values ( or elements) that are present in the current set and also referred to as the size of a set instance.

This function is similar to the size() and length() methods in other programming languages.

Syntax

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

public int Ds\Set::count()

Parameters

This function does not accept any parameter.

Return value

This function returns the number of values present in the current set.

Example 1

The following is the basic example of the PHP Ds\Set::count() function.

<?php
   $set = new \Ds\Set([1, 2, 3]);
   echo "The set elements are: \n";
   print_r($set);
   echo "The number of elements of set: ";
   print_r($set->count());
?>

Output

The above program produces the following output −

The set elements are:
Ds\Set Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)
The number of elements of set: 3

Example 2

The following is another example of the PHP Ds\Set::count() function. We use this function count the number of elements persent in this set.

<?php
   $set = new \Ds\Set(["a", "e", "i", "o", "u"]);
   echo "The elements are: \n";
   print_r($set);
   echo "The number of elements: ";
   print_r($set->count());
?>

Output

After executing the above example, it will display the following output −

The elements are:
Ds\Set Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
The number of elements: 5
php_function_reference.htm
Advertisements