簡體   English   中英

比較獲取數據並在 PHP 中使用 if else 語句

[英]comparing fetch data and using if else statement in PHP

獲取數據輸出時收到錯誤消息。 有些月有 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;

該代碼將不起作用,因為構造缺少括號......它應該是什么樣子的一種方式:

$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 會抱怨變量未定義。

我建議對數組做一些閱讀。 使用數組,您可以在數組變量中存儲多個值。 在這種情況下使用數組比使用數百個不同的變量要容易得多。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM