array_sum in PHP

Table of Contents

Syntax of array_sum

number array_sum(array array)

Returns the sum of every element in the array. If all of the values are integers, an integer is returned. If any of the values are floats, a float is returned

Example

<?php
$a = array(2, 4, 6, 8);
echo "sum(a) = " . array_sum($a) . "\n";

$b = array("a" => 1.2, "b" => 2.3, "c" => 3.4);
echo "sum(b) = " . array_sum($b) . "\n";
?>
sum(a) = 20
sum(b) = 6.9

Leave a Comment