# Thread: Division by zero help needed

1. ## Division by zero help needed

Hello all.

This code is throwing up errors:

PHP Code:
``` \$av = (\$Row[tons])/(\$Row[legs]); \$av = number_format(\$av,2);  ```
It throws an error when tons = 0 and legs = 0 (which sometimes,..has to be like that)

Is there anything I can do to improve it?

Matt

2. Yes. Test for a 0 value in the denominator, then skip the math if the denominator is equal to 0.

3. Can we see more of the code, or know what exactly this script is supposed to do? PHP will not do division by 0. Would this work?

PHP Code:
``` if (\$Row[tons] == "0" || \$Row[legs] == "0") {   \$av = 0; } else {   \$av = (\$Row[tons])/(\$Row[legs]);  }  ```

4. Hi.

You have to define a value which should be presented if \$Rows[legs] is equal to zero. In the code below, I used 'n/a'.

PHP Code:
``` // if you expect negative numbers change == to <= if (\$Row['legs'] == 0) {    \$av = 'n/a'; } else {    \$av = \$Row['tons'] / \$Row['legs'];    \$av = number_format(\$av,2);  }  ```

5. Hi Chris (and all others)

All working now thanks with no errors being thrown up.

Here's where it was being used if anyone was interested:

PHP Code:
``` \$Query = "SELECT * from \$TableName Order by Points desc";     \$Result = mysql_db_query(\$DBName, \$Query, \$Link);      // create a table to hold the results     print ("<table border=0 width=\"95%\" cellspacing=2 cellpadding=1 align=center>\n");     print ("<tr align=center valign=top>\n");     print ("<td align=center valign=top>&nbsp;</td>\n");     print ("<td align=center valign=top><b>Pld</b></td>\n");     print ("<td align=center valign=top><b>W</b></td>\n");     print ("<td align=center valign=top><b>L</b></td>\n");     print ("<td align=center valign=top><b>F</b></td>\n");     print ("<td align=center valign=top><b>A</b></td>\n");     print ("<td align=center valign=top><b>Pts</b></td>\n");     print ("<td align=center valign=top><b>Tons</b></td>\n");     print ("<td align=center valign=top><b>Legs</b></td>\n");     print ("<td align=center valign=top><b>Ave</b></td>\n");     print ("<td align=center valign=top><b>Pen</b></td>\n");     print ("</tr>\n");     print ("<tr>\n");     print ("<td colspan=11><hr></td>\n");     print ("</tr>\n");      // fetch the results from the database     while (\$Row = mysql_fetch_array(\$Result)) { if (\$Row[legs]==0){     \$av= 'n/a'; }else{     \$av = (\$Row[tons])/(\$Row[legs]);     \$av = number_format(\$av,2);     }                 print ("<tr align=center valign=top>\n");         print ("<td align=center valign=top>\$Row[name]</td>\n");         print ("<td align=center valign=top>\$Row[played]</td>\n");         print ("<td align=center valign=top>\$Row[won]</td>\n");         print ("<td align=center valign=top>\$Row[lost]</td>\n");         print ("<td align=center valign=top>\$Row[forr]</td>\n");         print ("<td align=center valign=top>\$Row[against]</td>\n");         print ("<td align=center valign=top>\$Row[points]</td>\n");         print ("<td align=center valign=top>\$Row[tons]</td>\n");         print ("<td align=center valign=top>\$Row[legs]</td>\n");         print ("<td align=center valign=top>\$av</td>\n");         print ("<td align=center valign=top>\$Row[penalty]</td>\n");         print ("<td>&nbsp;</td>\n");         print("<td align=center valign=top>&nbsp;</td>\n");         print ("</tr>\n");                  }                  mysql_close(\$Link);         print ("</table>\n");          ?>  ```

#### Posting Permissions

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