Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-29-2010, 05:54 PM   PM User | #1
jwp99
New to the CF scene

 
Join Date: Mar 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
jwp99 is an unknown quantity at this point
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!
jwp99 is offline   Reply With Quote
Old 03-29-2010, 05:57 PM   PM User | #2
masterofollies
Senior Coder

 
Join Date: May 2005
Posts: 2,137
Thanks: 96
Thanked 72 Times in 72 Posts
masterofollies can only hope to improve
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)
__________________
Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?
masterofollies is offline   Reply With Quote
Old 03-29-2010, 07:05 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 03-29-2010, 08:04 PM   PM User | #4
masterofollies
Senior Coder

 
Join Date: May 2005
Posts: 2,137
Thanks: 96
Thanked 72 Times in 72 Posts
masterofollies can only hope to improve
Something like that, but still if the value is 0 it's not going to work.
__________________
Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?
masterofollies is offline   Reply With Quote
Old 03-29-2010, 08:46 PM   PM User | #5
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,880
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by masterofollies View Post
It should be a problem with one of these

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

Quote:
Originally Posted by masterofollies View Post
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.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 03-29-2010, 09:06 PM   PM User | #6
masterofollies
Senior Coder

 
Join Date: May 2005
Posts: 2,137
Thanks: 96
Thanked 72 Times in 72 Posts
masterofollies can only hope to improve
Quote:
Originally Posted by Dormilich View Post
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.
Fou lu kind of already corrected it, please read the posts above.
__________________
Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?
masterofollies is offline   Reply With Quote
Old 03-29-2010, 09:12 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 03-29-2010, 09:22 PM   PM User | #8
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
The output of that code:

Quote:
PHP Warning: Division by zero in divisor.php on line 5
$z is: 0
++$z is: 0
MattF is offline   Reply With Quote
Old 03-29-2010, 09:23 PM   PM User | #9
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,880
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by masterofollies View Post
Fou lu kind of already corrected it, please read the posts above.
double reminder better sinks in.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 03-29-2010, 09:31 PM   PM User | #10
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by MattF View Post
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 03-30-2010, 05:34 PM   PM User | #11
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
Yup. Copied your code and the output verbatim.
MattF is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:30 AM.


Advertisement
Log in to turn off these ads.