array_sum() & array_product() in PHP.





array_sum():

array_sum() use for return the sum of all the values in the array. But the value must be numeric. But it can be either integer or floating value. Also it supports associative array to calculate sum. Let's see example below:


<?php
$x=array(2,3,5,7,9);
echo array_sum($x);
?>

The result will be : 26

Let's see another example below:

<?php
$y=array("a"=>1.2,"b"=>3.2,"c"=>1.6); //associative array and floating point value
echo array_sum($y);
?>

The result will be : 6


array_product():

array_product() use for return the product(multiplication) of an array. Again the value must be numeric. But it can be either integer or floating value. Let's see example below:


<?php
$x=array(2,3,5,7);
echo array_product($x);
?>

The result will be : 210


Let's see another example below:


<?php
$x=array(2.2,3.1,5.5,7.3); //using floating point value
echo array_product($x);
?>

The result will be : 273.823


 

Post a Comment (0)
Previous Post Next Post