简体   繁体   中英

comparing fetch data and using if else statement in PHP

I am getting an error message when getting the output of data. Some months have 4 weeks while others have 5 weeks. How can I easily find the average of each of this month?

My code looks like this:

$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;

That code won't work because the constructs are missing brackets... one way what it should look like:

$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;
}

Plus, you need to initialize each and every used variable. Otherwise, PHP will complain that the variable isn't defined.

I suggest to do some reading up on arrays. With an array, you can store multiple values in an array variable. Using arrays for cases like this is much easier than working with hundreds of different variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM