php循环加总和?

时间:2011-03-15 03:03:21

标签: php

对于计算总金额的变量,我该怎么用?

例如

if ($featured == "1"){
    $cost = 100;
}else{
    $cost = 1000;
}

基本上每次循环时,我们都会在页面上显示$cost,但我还想留出一个变量$total,在循环运行时将每个$cost相加。 / p>

1 个答案:

答案 0 :(得分:1)

对不起,但是真的吗?

$total = 0; // this somewhere before the loop

// start loop
if ($featured == "1") {
    $cost = 100;
} else {
    $cost = 1000;
}

$total += $cost;
// end loop