title: How to calculate total price in php
body: <p>I am getting the price of the product from db using the product id i have stored in php array.</p>
  
  <p>i have a php function where in i want to hold a total of all the products purchased. here i am getting the cart items and i am calling the function which should hold total amount. i am passing the value <code>price</code> each time when i retrieve the product from cart. </p>
  
  <p>for example for first item iteration i send value 300, and for second 400, for third 900.</p>
  
  <p>i want to add up all these and get total price of 1600 in <code>payableAmount()</code>
  how can i do this?</p>
  
  <pre><code>function getCartitems($product_id){
      $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
      if ($conn-&gt;connect_error) {
          die("Connection failed: " . $conn-&gt;connect_error);
      } 
      $sql = "SELECT product_id,product_name,product_price,product_image_url FROM product_list WHERE product_id='$product_id'";
      $result = $conn-&gt;query($sql);      
      if ($result-&gt;num_rows &gt; 0) {
          while($row = $result-&gt;fetch_assoc()) {              
          echo "&lt;tr&gt;&lt;td&gt;&lt;img class='cart-image img-responsive' src='".$row["product_image_url"]."'&gt;&lt;/td&gt;&lt;td&gt;".$row["product_name"]."&lt;/td&gt;&lt;td&gt;&lt;b&gt;&amp;#8377; &lt;/b&gt;".floor($row["product_price"])."&lt;/td&gt;&lt;td&gt;&lt;a href='mycart.php?action=remove&amp;product_id=".$row['product_id']."'&gt;&lt;span class='glyphicon glyphicon-remove'&gt;&lt;/span&gt; remove&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;";
          $price = $row['product_price'];
          payableAmount($price);      
          }
      } else {
          echo "There are no such items";
      }   
      $conn-&gt;close();
  }
  
  function payableAmount($totalPrice){    
     // calculate total price here
      $total = $totalPrice;
      echo $total;
  }
  </code></pre>