PHP people, problem with %

Discussion in 'OT Technology' started by zanyspy_dude, Feb 10, 2008.

  1. zanyspy_dude

    zanyspy_dude King of teh n00bz

    Joined:
    Aug 29, 2002
    Messages:
    4,473
    Likes Received:
    0
    Location:
    Indianapolis, IN
    I need to go through an array. I'm trying to do the good old...

    if(isset($index)){
    if($action == "Previous"){
    echo "Previous <br>";
    $index = ($index - 1) % $max;
    echo (-1) % (5);
    } else if($action == "Next"){
    echo "Next <br>";
    $index = ($index + 1) % $max;
    } else {
    }
    } else {
    $index = 0;
    echo "welcome!<br>";
    }

    the echo -1 % 5 returns -1 ALWAYS.

    yes -1 % 5 should be 4....see google for proof. What am i doing wrong?
    http://www.google.com/search?hl=en&q=-1+%+5&btnG=Google+Search
     
  2. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    php's modulus operator differs from the standard "mathmatical" modulus.

    you want:

    Code:
    <?php
    
    function mod($k, $m)
    {
            return ($k - $m * floor($k/$m));
    }
    
    echo mod(-1,5);
    
     
  3. zanyspy_dude

    zanyspy_dude King of teh n00bz

    Joined:
    Aug 29, 2002
    Messages:
    4,473
    Likes Received:
    0
    Location:
    Indianapolis, IN
    thank you SOOOO SOOO SOO O SOO much
     
  4. zanyspy_dude

    zanyspy_dude King of teh n00bz

    Joined:
    Aug 29, 2002
    Messages:
    4,473
    Likes Received:
    0
    Location:
    Indianapolis, IN
    so why is php's operator differnet?
     
  5. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    Same reason that MS's SQL interpreter doesn't need an End If, but VBA does. It makes no sense.
     
  6. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    not sure. Many bugs have been filed at PHP's bug tracker but they all get classified as "bogus" by php developers. All they say is "rtfm" but the manual doesn't explain why.
     

Share This Page