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: Warning: Division by Zero error

1. ## Warning: Division by Zero error

Our website was working perfectly fine for the last 6 months, and then just this month the page where we upload staff information has stopped working. We keep receiving a: Division by Zero error. Here is the area of the code that it says the error is in.....

if (\$NewHeight=='1' and \$NewWidth!='1') {
\$NewHeight = ceil((\$OldHeight*\$NewWidth)/\$OldWidth);
}
elseif (\$NewWidth=='1' and \$NewHeight!='1') {
\$NewWidth = ceil((\$OldWidth*\$NewHeight)/\$OldHeight);
}
elseif (\$NewHeight=='1' and \$NewWidth=='1') {
return(false);
}

\$OldHeight_castr = ceil((\$OldWidth*\$NewHeight)/\$NewWidth);
\$castr_bottom = (\$OldHeight-\$OldHeight_castr)/2;

\$OldWidth_castr = ceil((\$OldHeight*\$NewWidth)/\$NewHeight);
\$castr_right = (\$OldWidth-\$OldWidth_castr)/2;

Any help would be appreciated! Thanks so much!

2. It should be a problem with one of these

PHP Code:
``` \$castr_right = (\$OldWidth-\$OldWidth_castr)/2;  ```
Division by zero means exactly what it says, so if the value is 0 and you are trying to divide by 2, that doesn't work.

I'd say there is an error in getting the value (width or height), or else it's one or the other (such as 100px height, 0px width)

3. Other way around mate; you can divide 0 by anything, but you cannot divide anything by 0.
So the problem will be with one of these:
\$NewHeight = ceil((\$OldHeight*\$NewWidth)/\$OldWidth);
\$NewWidth = ceil((\$OldWidth*\$NewHeight)/\$OldHeight);
\$OldHeight_castr = ceil((\$OldWidth*\$NewHeight)/\$NewWidth);
\$OldWidth_castr = ceil((\$OldHeight*\$NewWidth)/\$NewHeight);

\$OldWidth, \$OldHeight, \$NewWidth, or \$NewHeight will be 0. Start debugging or add print statements to see which one(s) is/are zero.

4. Something like that, but still if the value is 0 it's not going to work.

5. Originally Posted by masterofollies
It should be a problem with one of these

PHP Code:
``` \$castr_right = (\$OldWidth-\$OldWidth_castr)/2;  ```
always solvable.

Originally Posted by masterofollies
Division by zero means exactly what it says, so if the value is 0 and you are trying to divide by 2, that doesn't work.
may I remind you of basic math? zero divided by anything but zero always gives … zero.

dividing by zero is: `y = x/0;` which is not declared.

6. Originally Posted by Dormilich
always solvable.

may I remind you of basic math? zero divided by anything but zero always gives … zero.

dividing by zero is: `y = x/0;` which is not declared.

7. It should be noted in PHP though that \$x / 0 should actually work. Aside from triggering the error itself (which is just a warning mind you), it should return null if my assumption is correct. Attempting to use that in a numerical context will actually allow you to operate on it (or should if my initial assumptions are correct).
PHP Code:
``` \$x = 1; \$y = 0; \$z = \$x / \$y; // Warning: div..... \$x = null printf("\\$z is: %d\n", \$z); // \$z is: 0 printf("++\\$z is: %d\n", ++\$z); // ++\$z is: 1  ```
Does that work out to one? Anyone that can test that? I assume that ++null = 1, though --null = well null. Hah.

8. The output of that code:

PHP Warning: Division by zero in divisor.php on line 5
\$z is: 0
++\$z is: 0

9. Originally Posted by masterofollies
double reminder better sinks in.

10. Originally Posted by MattF
The output of that code:
Really? I'm a little suprised; the API states that null can be incremented to one (but not decremented, or I guess more accurately null is decremented to null). I'd have expected that ++\$z would result in 1.

11. Yup. Copied your code and the output verbatim.

#### Posting Permissions

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