获取数据输出时收到错误消息。有些月份有4周,有些则有5周。我怎样才能轻松找到本月每个月的平均值?
我的代码如下所示:
$avrtot1 = ($total1 + $total2 + $total3 + $total4);
$avrtot2 = ($total1 + $total2 + $total3 + $total4 + $total5);
$total5 = ($boy1wk5 + $girl1wk5 + $boy2wk5 + $girl2wk5);
if ($total5 = 0) {
$avrtot1 = (($total1 + $total2 + $total3 + $total4)/4)
}
else
$avrtot2 = ($total1 + $total2 + $total3 + $total4 + $total5)/5;
答案 0 :(得分:0)
该代码不起作用,因为构造缺少括号......它应该是这样的一种方式:
$avrtot1 = ($total1 + $total2 + $total3 + $total4);
$avrtot2 = ($total1 + $total2 + $total3 + $total4 + $total5);
$total5 = ($boy1wk5 + $girl1wk5 + $boy2wk5 + $girl2wk5);
if ($total5 = 0) {
$avrtot1 = (($total1 + $total2 + $total3 + $total4)/4);
} else {
$avrtot2 = ($total1 + $total2 + $total3 + $total4 + $total5)/5;
}
另外,您需要初始化每个使用过的变量。否则,PHP会抱怨变量没有被定义。
我建议对阵列进行一些阅读。使用数组,您可以在数组变量中存储多个值。对于像这样的情况使用数组要比使用数百个不同的变量容易得多。