By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,722 Members | 1,904 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,722 IT Pros & Developers. It's quick & easy.

Error: Divide by 0

P: 5
Ok so I am working on a page: http://imageparticle.com/apache/Draft%201/newthesis.php

This is really my first time using php, but anyway, basically the thing takes the input from the first page, and then shows the % change from year to year on the next page for each category (cash, etc)

the PROBLEM right now is that when it does this, and the user hasnt filled in every single input, it gives div by 0 errors.

I know perfectly well why this happens, however i'm not quite sure how to fix it? Ideally i would just like to set all of the default values to 1 instead of 0.... as this would eliminate the problem, however, i'm not sure how to do that? Any help would be appreciated. Thanks.
Feb 17 '07 #1
Share this Question
Share on Google+
5 Replies


P: 5
Actually after thinking about it, the default value doesnt matter, people need to be able to enter 0's.

So for example if for 3 years, Accounts Receivable were 200, 0, 0 respectively... then the % change would be -100%, and 0%.... but without the error div by 0...?

Also, if it were $0, $0, $200... %'s would need to be 0% and 100%.

Thank you in advance
Feb 17 '07 #2

Atli
Expert 5K+
P: 5,058
Hi.

I have no clue what your calculating there but you could fix it with a simple if statment, that checks if the field is 0 before you calculate.

Like this.
[PHP]
$var = 0;

if(abs($var) != 0)
{
// Do your calculations
}
else
{
// Do some other calculations
}
[/PHP]

It is also possible to catch an exception in PHP 5, similar to what many other languages offer. But that is much more complex.

Click here to see the manual at php.net
Feb 18 '07 #3

P: 5
[PHP]
function change($x,$y) //This function takes the input of 2 numbers and calculates the % change
{
$changeout= (($y - $x) / $x) * 100;
if ($changeout>0)
echo "<FONT color='#41A317'><B>" . number_format($changeout,2) . "%" . "</B></FONT>";
elseif ($changeout==0)
echo "<B>" . number_format($changeout,2) . "%" . "</B>";
else
echo "<FONT color='#FF0000'><B>(" . -number_format($changeout,2) . "%)" . "</B></FONT>";
}
?>[/PHP]

That is the code i'm talking about... the problem happens when $changeout= (($y - $x) / $x) * 100; is calculated and $x is = 0. Is there a way to write the function so that if x==0 it just returns 0%? I tried to make it do that, however it was screwing up the current IF loop
Feb 18 '07 #4

P: 5
woohoo. Ok well i sorta took your advice, it inspired me anyway. I split it into two functions. The first checks for 0's and handles accordingly, the second changes the color.


[PHP]<?php

function change($x,$y) //This function takes the input of 2 numbers and calculates the % change
{
if (abs($x)!=0)
{
$changeout= (($y - $x) / $x) * 100;
changecolor($changeout);
}
else
{
echo "<B>" . "-" . "</B>";
}
}

function changecolor($col) //changes the color of the output
{
if ($col>0)
echo "<FONT color='#41A317'><B>" . number_format($col,2) . "%" . "</B></FONT>";
elseif ($col==0)
echo "<B>" . number_format($col,2) . "%" . "</B>";
else
echo "<FONT color='#FF0000'><B>(" . -number_format($col,2) . "%)" . "</B></FONT>";
}

?>[/PHP]

that is the code in case anyone was interested
Feb 18 '07 #5

ronverdonk
Expert 2.5K+
P: 4,258
Thanks for showing us your solution to this.

Ronald :cool:
Feb 18 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.