通过循环php简单计数?

时间:2012-09-04 07:08:19

标签: php variables loops

我有一个简单的问题,我不太确定如何使用循环来获取php变量并将它们添加到一起

假设我有这个循环

$total = 0;
while ( $foo ='bar' );
    $amount = meta($row, 'amount');
endwhile;

$total = 'NEW AMOUNT';

我的问题是,如何添加金额(1到200之间)以创建底部$total?来自while ( $foo ='bar');的对象数量不断增长,可以是2个总对象,或2000个。

2 个答案:

答案 0 :(得分:2)

添加到$total并使用;更改while前面的:以启动while

$total = 0;
while ( $foo = 'bar' ):
    $total += meta($row, 'amount'); // assuming `meta()` is one of your functions that extract `amount` from someplace ..
endwhile;

echo $total;

答案 1 :(得分:2)

$total = 0;
while ( $foo ='bar' )
{
    $amount = meta($row, 'amount');
    $total = $total + $amount;
}

这样的东西? 如果$total$amount都是整数,则可以将它们一起添加。

我还假设meta()是计算金额并将其返回的方法吗?

注意我是如何更改while循环(添加大括号)的,在结束大括号之后你应该有$total变量及其中的总金额。