PHP | Ds\Deque sum() Function

The Ds\Deque::sum() function is an inbuilt function in PHP which is used to return the sum of all elements present in the Deque.

Syntax:

public Ds\Deque::sum( void ) : number

Parameters: This function does not accept any parameter.



Return Value: This function returns the sum of all Deque elements.

Below programs illustrate the Ds\Deque::sum() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declare a deque
$deck = new \Ds\Deque([5, 6, 3, 2, 7, 1]);
  
echo("Elements of Deque\n");
  
// Display the Deque elements
print_r($deck);
  
echo("Sum of all Deque elements: ");
  
// Use sum() function to add all
// Deque elements
var_dump($deck->sum());
  
?>

chevron_right


Output:

Elements of Deque
Ds\Deque Object
(
    [0] => 5
    [1] => 6
    [2] => 3
    [3] => 2
    [4] => 7
    [5] => 1
)
Sum of all Deque elements: int(24)

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declare a deque
$deck = new \Ds\Deque([1.5, 6.3, 2.3, 7.2, 4.7, 9.3]);
  
echo("Elements of Deque\n");
  
// Display the Deque elements
print_r($deck);
  
echo("Sum of all Deque elements: ");
  
// Use sum() function to add all
// Deque elements
var_dump($deck->sum());
  
?>

chevron_right


Output:

Elements of Deque
Ds\Deque Object
(
    [0] => 1.5
    [1] => 6.3
    [2] => 2.3
    [3] => 7.2
    [4] => 4.7
    [5] => 9.3
)
Sum of all Deque elements: float(31.3)

Reference: http://php.net/manual/en/ds-deque.sum.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.