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

PHP - Ds\Queue::count() Function



The PHP Ds\Queue::count() function is used to retrieve the count of elements present in a queue.

The "count" of elements refers to the total number of elements in the queue, which is similar to the size in other programming languages. If the current queue is empty ([]), this function will return a count of zero (0).

Syntax

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

public Ds\Queue::count(): int

Parameters

This function does not accept any parameter.

Return value

This function returns the number of elements present in a queue.

Example 1

The following program demonstrates the usage of the PHP Ds\Queue::count() function −

<?php  
   $queue = new \Ds\Queue([10, 20, 30, 40, 50]);
   echo "The queue elements are: \n";
   print_r($queue);
   echo "The number of elements present in the queue: ";
   print_r($queue->count());
?>

Output

The above program returns the number of elements in the queue −

The queue elements are:
Ds\Queue Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)
The number of elements present in the queue: 5

Example 2

If the current queue is empty ([]) of no element present, the PHP Ds\Queue::count() function will return zero (0) as count −

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

Output

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

The queue elements are:
Ds\Queue Object
(
)
The number of elements present in the queue: 0
php_function_reference.htm
Advertisements