2013-07-26 09:25

# 在php中将for循环的变量返回值加起来

I have this code that is working perfectly. My question is how can I sum all the value of \$manpowerCost and \$otherCharges?

``````for (\$i = 1; \$i<=9; \$i++) {
\$m = '0'.\$i;
\$sumMc = array();
\$revenue = revenue(\$project, \$year.'-'.\$m);
\$manpowerCost = manpowerCost(\$project, \$year.'-'.\$m);
\$otherCharges = otherCharges(\$project, \$year.'-'.\$m);
\$netIncome = netIncome(\$revenue, \$manpowerCost, \$otherCharges);

if (\$manpowerCost<=0 && \$m <= \$projected_date){
\$manpowerCost = round((\$projected_mc * \$percentage_mc),2);
\$projected_total_mc += \$manpowerCost;
\$pt = \$pt + \$manpowerCost;
} else {
\$projected_mc = \$manpowerCost;
\$manpowerCost = \$projected_mc;
}

if (\$otherCharges<=0 && \$m <= \$projected_date){
\$otherCharges = round((\$projected_oc * \$percentage_oc),2);
\$projected_total_oc += \$otherCharges;
\$pt = \$pt + \$otherCharges;
} else {
\$projected_oc = \$otherCharges;
\$otherCharges = \$projected_oc;
}
echo    '<td>RE:'.number_format(\$revenue, 2, '.', '').'<br />MC:'.number_format(\$manpowerCost, 2, '.', '').'<br />OC:'.number_format(\$otherCharges, 2, '.', '').'<br />NI:'.\$netIncome.'</td>';
}
``````
• 点赞
• 写回答
• 关注问题
• 收藏
• 复制链接分享
• 邀请回答

#### 2条回答

• U can Try like this

``````\$powercost_total = '0';
for (\$i = 1; \$i<=9; \$i++) {
\$manpowerCost = manpowerCost(\$project, \$year.'-'.\$m);
\$powercost_total = \$manpowerCost + 4powercost_total;
....
``````
点赞 评论 复制链接分享
• Declare \$TotalManPowerCost and \$TotalOtherCharges variables outside of the loop, and add \$manpowercost and \$otherCharges to them in each iteration of the loop

点赞 评论 复制链接分享