Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Division by zero error how can i get rid of it?

1. ## Division by zero error how can i get rid of it?

hi there
i need some help with this , it just keep giving this error here
any ideas it how i can solve it?

Warning: Division by zero
...on line xxx

\$buf .= "</tr> \n";
\$tpl->set_var('lstemitidosmes',\$buf);
\$tpl->set_var('mesemissao',\$totaisOrdem[count(\$totaisOrdem)-1]['MONTH
']);
\$tpl->set_var('anoemissao',\$totaisOrdem[count(\$totaisOrdem)-1]['YEAR']);
/*getting error on this line*/ \$valorPerc = ROUND(((\$totaisOrdem[count(\$totaisOrdem)-1]['VLR']/\$totaisOrdem[count(\$totaisOrdem)-2]['VLR'])-1)*100,2);
\$tpl->set_var('aumentonomescartdsc',((\$valorPerc < 0) ? "got lower":"rised"));
\$valorPerc = (\$valorPerc < 0) ? (\$valorPerc * -1):\$valorPerc;
\$tpl->set_var('aumentonomescart', \$valorPerc );

2. You stop dividing by 0.
PHP Code:
``` if (0 < (\$denom = \$totaisOrdem[count(\$totaisOrdem)-2]['VLR'])-1)*100,2)) {     \$valorPerc = ROUND(((\$totaisOrdem[count(\$totaisOrdem)-1]['VLR']/\$denom; }  ```
Don't use suppression to avoid the errors, always fix them.

If the denominator is never supposed to be 0, you'll need to debug your code to find out why it is 0.

3. Originally Posted by Fou-Lu
You stop dividing by 0.
PHP Code:
``` if (0 < (\$denom = \$totaisOrdem[count(\$totaisOrdem)-2]['VLR'])-1)*100,2)) {     \$valorPerc = ROUND(((\$totaisOrdem[count(\$totaisOrdem)-1]['VLR']/\$denom; }  ```
Don't use suppression to avoid the errors, always fix them.

If the denominator is never supposed to be 0, you'll need to debug your code to find out why it is 0.
it worked thank you , but i created var \$denom called it on if , that sentence was giving an error at multiplication here

4. I don't see any problem with the multiplication (its quite rare for something that cannot evaluate to a number in PHP), though I do see bracket mismatching here. Guess I shouldn't have just cut and paste parts out of it:
PHP Code:
``` \$valorPerc = 0; if (0 < (\$denom = \$totaisOrdem[count(\$totaisOrdem)-2]['VLR']))  {     \$valorPerc = ROUND(((\$totaisOrdem[count(\$totaisOrdem)-1]['VLR']/\$denom)-1)*100,2); }  ```
Ok, that looks like its right. I initialized denom before the branch since it would otherwise become null outside in the event of a non-evaluation.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•