title: PHP - How to use the sum variable inside a loop
body: <p>I just wanted to get the sum of integers inside my <code>foreach</code> loop.
  Typically we can get the sum by this kind of syntax</p>
  
  <pre><code>$sum = 0;
  foreach($group as $key=&gt;$value)
  {
     $sum+= $value;
  }
  echo $sum;
  </code></pre>
  
  <p>by <code>echoing</code> the <code>$sum</code> variable we can get the sum outside of the loop.</p>
  
  <p>But I wanted to use the <code>$sum</code> variable inside the <code>foreach</code> loop.
  I'm having some computation in my program. And I need use the <code>$sum</code> variable inside the loop itself.</p>
  
  <p>here are the values inside <code>$sum</code></p>
  
  <pre><code>1260.32871
  9689.77324
  10760.87267
  126247.29657
  151455.92761
  198485.57421
  223694.20525
  334296.14836
  348160.79259
  352887.41091
  367760.42095
  466451.71783
  471178.33615
  471966.10587
  473856.85604
  475416.45497
  </code></pre>
  
  <p>The sum is <code>475416.45497</code> which is of course at the last. the thing is I'm having computation inside the loop. Since it's iterating. I can still get the previous values starting from the top <code>1260.32871</code> while having my computation. I just wanted to get the <code>475416.45497</code> which is at the very bottom.</p>