How to use array sum in PHP

In this article, I will explain how the array_sum() function can be used in PHP.
  • 1668

array_sum() function in PHP

  • The array_sum() function calculate the sum of values in an array

  • The array_sum() function returns the sum of all the values in the array.

Syntax

array_sum(array)

Parameter

  • array  array is required parameter. Specifies an array.

Example

The following example show to how the array_sum() function can be used in PHP.

<html>

<body>

<h3 style="color: slategrey;">array_sum() function example in PHP</h3>  

    <?php

    $a = array(1, 2, 3, 4);

    echo "sum of array1(a) = " . array_sum($a) . "<br />";

    $b = array("a" => 2.1, "b" => 3.3, "c" => 4.4);

    echo "sum of array2(b) = " . array_sum($b) . "<br />";

    ?>

</body>

</html>

 

Output

sum.jpg

You may also want to read these related articles here
 
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.