Skip to content
Related Articles

Related Articles

Improve Article

PHP | Ds\Deque sum() Function

  • Last Updated : 14 Aug, 2019

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:




<?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());
  
?>
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:




<?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());
  
?>
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
Recommended Articles
Page :