Error: Divide by 0

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cooltoad
    New Member
    • Feb 2007
    • 5

    Error: Divide by 0

    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.
  • cooltoad
    New Member
    • Feb 2007
    • 5

    #2
    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

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      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

      Comment

      • cooltoad
        New Member
        • Feb 2007
        • 5

        #4
        [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

        Comment

        • cooltoad
          New Member
          • Feb 2007
          • 5

          #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($ch angeout);
          }
          else
          {
          echo "<B>" . "-" . "</B>";
          }
          }

          function changecolor($co l) //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

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Thanks for showing us your solution to this.

            Ronald :cool:

            Comment

            Working...