SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Location
    California
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How serious is a 'division by zero' error?

    I use WAMP and my Apache service recently stopped working. I posted my Apache error log on the official WAMP forums and someone noted that one of the last things to occur was a 'division by zero' error in PHP. He said that this is often a fatal error and could be the reason the Apache service is not working. Is this true? Is a 'division by zero' error really that serious?

    Thanks!

  2. #2
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    67 Post(s)
    Tagged
    2 Thread(s)
    Serious enough to throw a fatal error in a PHP script, but that won't crash your WAMP server.

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    'fatal' is relative to scope.

    It was fatal to the execution of your script, yes. But not fatal to the operation of the WAMP server.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  4. #4
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Division by zero does not cause fatal error in PHP, it only generates a warning and the script execution is continued.

  5. #5
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,053
    Mentioned
    66 Post(s)
    Tagged
    0 Thread(s)
    True - note the following...

    First, Servers can be configured to stop on warnings - and/or Apache may be logging the error and that's what mattSanchez has noted.
    Second, if the PHP is trying to create a well-formed response - say stream a video or image, or generate an XML or JSON response, the emission of the warning could certainly destroy the integrity of the file since by default PHP injects text of the warning into the output immediately upon encountering it. Such a bad response could cause a fatal error in the code relying on the response.

    Star is correct that such an error cannot bring the whole webserver down. However, any access of that particular script may be fruitless until the error is removed.

    Finally, keep in mind that PHP's default behavior is to return null on an illegal operation. Hence if your error occurs on a line like this.

    Code php:
    $a = $b / $c;

    $a will be assigned null when $c is zero. If it is numerically evaluated that null will be treated as 0. So, in a sense, division by zero in PHP yields 0 in a roundabout way, but avoiding the error by preventing the situtation - for example.

    Code php:
    $a = $c == 0 ? 0 : $b / $c;


    or for those who dislike ternaries.

    Code php:
    if ($c == 0) {
      $a = 0;
    } else {
      $a = $b / $c;
    }

  6. #6
    SitePoint Member blackrash's Avatar
    Join Date
    Jan 2013
    Location
    MILLCREEK,WA,USA
    Posts
    5
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes i agree with lemon juice.It is not a fatal error infact it is just a error and it generates script execution and nothing to worry about.

  7. #7
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,053
    Mentioned
    66 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blackrash View Post
    Yes i agree with lemon juice.It is not a fatal error infact it is just a error and it generates script execution and nothing to worry about.
    Broken JSON and XML files would fall under the heading of something to worry about. Server settings can be used to suppress the error, but I still would see such an error as cause for concern because if a programmer is careless enough to let a division by zero error remain in the code there are probably far more serious and dangerous lapses hiding in the wings.


Bookmarks

Posting Permissions

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