Forum Moderators: coopster

Message Too Old, No Replies

Warning: Division by zero in

hor to rid Warning: Division by zero in

         

weddingm

1:22 am on Oct 27, 2009 (gmt 0)

10+ Year Member



I need help getting rid of this error. I know that it shows because I am dividing by zero...however, I entered the code below and it still shows

Any help appreciated:


if (($responseqty>0) && ($responsesum>0))
{$responseavg=($responsesum/$responseqty);}
else {$responseavg=0;}

I have also tried:


$responseavg=@($responsesum/$responseqty);

Thanks,
Matt

Zipper

2:56 am on Oct 27, 2009 (gmt 0)

10+ Year Member



The code looks fine, so I'm not quite sure what's wrong. But from experience, a common cause for this kind of warning is misspelling variable names. You're variable names are hard to read and could easily be misspelled when you declared them. So you might want to double check on that.

rocknbil

7:12 pm on Oct 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What do you get when you echo $responseqty just before the "if?"

LifeinAsia

7:21 pm on Oct 27, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Not sure about PHP, but in mNy programming languages, if $responseqty is null or a non-numeric value, "$responseqty>0" would evaluate to TRUE, but "/$responseqty" would be a divide by zero error. Like the good rocknbill suggested- echo $responseqty to see the exact value of it.

samplephpcodes

6:40 am on Oct 28, 2009 (gmt 0)



if (($responseqty>0))
{
$responseavg=($responsesum/$responseqty);
}
else {$responseavg=0;}

Try this :)
Visit <snip> for more useful PHP scripts

[edited by: dreamcatcher at 7:07 am (utc) on Oct. 28, 2009]
[edit reason] No self promotion. See TOS. [/edit]

skinsey

8:59 am on Oct 28, 2009 (gmt 0)

10+ Year Member



if (($responseqty>0 ¦¦ $responseqty = NULL) && ($responsesum>0 ¦¦ $responsesum = NULL))
{$responseavg=($responsesum/$responseqty);}
else {$responseavg=0;}

rocknbil

4:52 pm on Oct 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For anyone reading this, this is a common error I've made myself:

if (($responseqty>0 ¦¦ $responseqty = NULL) && ($responsesum>0 ¦¦ $responsesum = NULL))

With the single =, you are setting the values of $responseqty and $responsesum, and with the logical "or", it doesn't matter what the first conditional is, it will always return true.

if (($responseqty>0 ¦¦ $responseqty == NULL) && ($responsesum>0 ¦¦ $responsesum == NULL))

Even if this worked, it's going to try to divide by NULL. Huh? :-)

Null should never be greater than 0 anyway, but see L.I.A.'s recommendation, and again, what does it echo prior to your "if?"

skinsey

8:45 pm on Oct 28, 2009 (gmt 0)

10+ Year Member



Yes, rocknbil was right.
I can't beleive I actually did that. Anyway here's the actual fix. This accounts for everyone's logic from the topic. All three examples will work and the original is fine also.

The correct answer of fixing the problem for this issue came from
Zipper
Because the original code was fine. Most likely just needed to reupload the code after the fix that they posted.

//Fix just check quanity
if ($responseqty>0)
{
$responseavg=($responsesum/$responseqty);
}
else {$responseavg=0;
}

//Fix by checking both varaiable
if ($responseqty>0 && $responsesum>0)
{
$responseavg=($responsesum/$responseqty);
}
else {$responseavg=0;
}

//original code that works
if (($responseqty>0) && ($responsesum>0))
{$responseavg=($responsesum/$responseqty);}
else {$responseavg=0;}

//Fix to account for is LIA issue of null is true
if (($responseqty>0 && $responseqty != NULL) && ($responsesum>0 && $responsesum != NULL))
{
$responseavg=($responsesum/$responseqty);
}
else {
$responseavg=0;
}