SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict foolios's Avatar
    Join Date
    Dec 2006
    Location
    The least technologically advanced state in the US
    Posts
    202
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    division by zero

    I get the error:

    Warning: Division by zero in P:\wamp\www\index.php on line 78

    line 78:

    $percentRead = round($mr1s / $mr1t, 2);


    For the following code:

    if ($_POST["mr1t"] & $_POST["mr1s"])
    {
    $total = $_POST["mr1t"];
    $skips = $_POST["mr1s"];
    echo "<h4> Success! </h4> $mr1 read $mr1t total items. $mr1 skipped $mr1s items during this run. <br />";
    $percentRead = round($mr1s / $mr1t, 2);
    echo "The percentage of meters skipped by $mr1 is: $percentRead &#37;";

    echo $_POST["mr1"];
    echo $_POST["mr1s"];
    echo $_POST["mr1t"];

    }

    When I echo out the variables, they have the values in them. I am not inputting a zero in the division of the variables. I am trying things like 100/25. This code worked on a live server, but is not working on my personal Wamp server.

    The search results haven't turned up any posts of config settings for apache or php but coding problems. This code looks functional to me.
    Any help would be greatly appreciated. Thanks in advance.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your code relies on register_globals.

    $mr1s and $mr1t will not be defined if this setting is off. It's recomended your code not rely on this setting.

    define the variables
    PHP Code:
    $mr1s $_POST["mr1s"];
    $mr1t =  $_POST["mr1t"]; 
    also, the '&' operator is a bitwise operator, not the logical 'AND' operator. You most likely meant to use '&&'

  3. #3
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by foolios View Post
    This code worked on a live server, but is not working on my personal Wamp server.
    That's because a lot of servers are set up to accommodate bad code, and WAMPServer is set up by default fairly well (no register_globals, no short_tags, no magic_quotes IIRC).

  4. #4
    SitePoint Addict foolios's Avatar
    Join Date
    Dec 2006
    Location
    The least technologically advanced state in the US
    Posts
    202
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the correction on the & to &&.

    I see what you are saying now about the globals. I was accessing them directly instead of the variables I can use to store their values. Wups, I oversighted that one. Thanks for showing me the mistake.
    Last edited by foolios; Jul 5, 2008 at 10:22.


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
  •