我遇到了一个简单的问题,我不太确定如何使用循环来获取php变量并将它们添加在一起
假设我有这个循环
$total = 0;
while ( $foo ='bar' );
$amount = meta($row, 'amount');
endwhile;
$total = 'NEW AMOUNT';
我的问题是,如何将数量(从1到200)相加以创建底部$total
?来自while ( $foo ='bar');
的对象数量不断增长并且可以是总共2个对象或2000个。
添加到$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;
$total = 0;
while ( $foo ='bar' )
{
$amount = meta($row, 'amount');
$total = $total + $amount;
}
像这样的东西?如果$total
和$amount
都是整数,您可以将它们相加。
我还假设meta()
是一种计算金额并返回的方法?
注意我是如何更改while循环(添加大括号)的,在结束大括号之后,应该有$total
变量和其中的总量。