array sum function - sum the elements of an array

array_sum function - sum the elements of an array

array-sum-example-in-php.php


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title>array_sum function - sum the elements of an array</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="Author" content="Md Iqbal Hosan">
  <meta name="title" content="array_sum function - sum the elements of an array">
  <meta name="keywords" content="php, example, code, array, function, array_sum">
  <meta name="description" content="array_sum function - sum the elements of an array">
 </head>
 <body>
  <h3>array_sum function - sum the elements of an array</h3>
  <?php
   /*
    ------------------------------------------------
    let us make an array fill up with some values
    ------------------------------------------------
   */
   $myArray = array(10,20,30,40,50);
   echo "<p style='background:#eee; color:blue;'>Output of myArray</p>";
   echo "<pre>";
   print_r($myArray);
   echo "</pre>";
   /*
    -------------------------------------------------------------------------------------
    we want to sum all the elements of the array by using array_sum function
    -------------------------------------------------------------------------------------
   */
   $arraySum= array_sum($myArray);
   echo "<p style='background:#ddd; color:blue;'>Output of arraySum using array_sum function</p>";
   echo "<pre>";
   print_r($arraySum);
   echo "</pre>";
   /*
    -----------------------------------------------------------
    let us make another array fill up with some float values
    -----------------------------------------------------------
   */
   $myArray2 = array(10.1,20.2,30.3);
   echo "<p style='background:#ccc; color:blue;'>Output of myArray2</p>";
   echo "<pre>";
   print_r($myArray2);
   echo "</pre>";
   /*
    -------------------------------------------------------------------------------------
    we want to sum all the float elements of the array by using array_sum function
    -------------------------------------------------------------------------------------
   */
   $arraySum2= array_sum($myArray2);
   echo "<p style='background:#aaa; color:blue;'>Output of arraySum2 using array_sum function</p>";
   echo "<pre>";
   print_r($arraySum2);
   echo "</pre>";
  ?>
 </body>
</html>

array_sum function - sum the elements of an array - output in the browser

No comments:

Post a Comment

leave your comments here..