madclarinet (madclarinet) wrote,
madclarinet
madclarinet

Programming Rule No. 43

Spot the silly mistake

FOR($k=0;$k < $num_columns;$k++) {
$col_total = 0;
reset($qdata);
while(list($k,$v)=each($qdata)) {
$test = explode(",",$v);
IF ($test[0] == $data[$k] || $test[1] == $data[$k]) {
$col_total = $col_total + $test[2];
}
}


When using loops within loops ensure you don't use the the same variable name as part of the loop - you end up with a runaway process. Took 20 minutes to find as well with numerous apache restarts.

This is the correct (and now working) part

FOR($l=0;$l < $num_columns;$l++) {
$col_total = 0;
reset($qdata);
while(list($k,$v)=each($qdata)) {
$test = explode(",",$v);
IF ($test[0] == $data[$l] || $test[1] == $data[$l]) {
$col_total = $col_total + $test[2];
}
}

My reaction when I realised this 'oops'
Subscribe
  • Post a new comment

    Error

    default userpic

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments