Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Calculate sum and product of all values in the array in php?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 160
    Comment on it

    Hi Reader's,
    Welcome to FindNerd, today we are going to discuss how to Calculate sum and product of all values in the array in php?

    Calculate sum and product are very important features when we need calculate sum or product of values stored in an array.

    There are two built in functions to calculate sum and product of array values in php.

    1-array_sum() Function :-This function calculate the total sum of array values and returns the sum of all the values in the array.

    Syntax of array_sum() Function

    array_sum(array)

    array is a "Required" parameter which is specifies an array.

    You can see below example

    <?php
      //define here a variable of array
      $Sum_arr = array(5, 8, 7, 20);
      echo "Total Sum : ".array_sum($Sum_arr);
    ?>
    

    Output will come following
    Total Sum : 40

    2-array_product() Function:-array_product() function calculate the total product of array values and returns the product of all the values in the array.

    Syntax of array_product() Function

    array_product(array)

     Description : array is a "Required" parameter which is specifies an array.

    Note : array_product() return 1 for empty array in PHP version 5.3.

    You can see below Example
     

    <?php
      //define here a variable of array
      $Prod_arr = array(3, 5, 4, 8);
      echo "Product Result : ".array_product($Prod_arr);
    ?>

    Output will come following
      Product Result : 480

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: