Php 循环开始时对值求和一次

Php sum value once when loop start

你好我在变量中有一个值我想在循环开始时第一次添加这个值并不总是。例如

<?php

   $balance = 100;
   while($row = mysql_fetch_array($query)
   {
      echo $row['amount'] + $balance; // for example $row['amount'] is 50, 20 , 30;
   }
 ?>

我想要的结果如下

<?php       

   150
   170
   200

?>

最简单的方法

<?php

   $balance = 100;
   while($row = mysql_fetch_array($query)
   {
      $balance = $balance + $row['amount'];
      echo $balance;
   }
 ?>

示例代码..

$balance = 100;
$counter=1;
$total=0;
while($row = mysql_fetch_array($query)
{
    if($counter==1)
    {
        $total=$row['amount'] + $balance; // for example $row['amount'] is 50, 20 , 30;
    }
    else
    {
        $total = $row['amount']+$total;
    }

    echo $total.'<br>';

    $counter++;
}