mrplatts Posted November 16, 2006 Share Posted November 16, 2006 Bear with me, I'm learningI understand what modulus is, and the basic workings of it ie [edit]3 %7 is 1 OK, Why does this: [code]$r =$r++ % 6 + 1; [/code]increment 1, 2, 3, 4, 5, 6, 1, 2, 3 ,4, 5, 6I got it from someone on here & it works great. I want to know why. Maybe I don't understand the order of operations in this case... Rich Link to comment https://forums.phpfreaks.com/topic/27448-understanding-modulus/ Share on other sites More sharing options...
printf Posted November 16, 2006 Share Posted November 16, 2006 Thing are evaluated as they are written, but each equation is defined by the maybe the position of the brackets or other conditions you may have in the equation..like this example[code]<?phpfor ( $i = 0; $i < 12; $i++ ){ echo $i % 6 + 1;}?>[/code]It's the same as your example, but instead of the increment++ being in the equation it is defined and incremented in the loop control! So for your example...[code]$r++ % 6[/code]Is evaluated first, meaning return the percentage that [b]$r[/b] is of [b]6[/b], then [b]+[/b], add 1 to that returned percentage and then assign that value to [b]$r[/b]! Link to comment https://forums.phpfreaks.com/topic/27448-understanding-modulus/#findComment-125527 Share on other sites More sharing options...
impulse() Posted November 16, 2006 Share Posted November 16, 2006 In basic terms it's the same as 'left over' that you learn in school mathematics.7 mod 15 = 1.It equals 1 because 7 goes into 15 twice and you're left over with 1.7 mod 14 = 0.It equals 0 because 7 goes into 14 twice and you're left with 0.Hope that helps, Link to comment https://forums.phpfreaks.com/topic/27448-understanding-modulus/#findComment-125532 Share on other sites More sharing options...
mrplatts Posted November 16, 2006 Author Share Posted November 16, 2006 ??? I understand that I transposed the units in the example I gave... 3%7=1OK -- this is the code below... I think I follow the example given by printf but am not sure if it applies to this example exactly. Like I said, it works perfectly. Before I got the idea from someone on this forum, I had a cumbersome set of if checks to reset to one if the rotation number reached 7. Since this is way sleeker I want to 'get it' [code]<?phpfunction rotation_inc(&$r, $current_date, $excepts) //rotation, $day of week{ $t = date("Y-m-d", $current_date);//mySQL formatted date yyyy-mm-dd$d = date('w',$current_date); //gives day of week a number value 0 - 6if (!in_array($t, $excepts) && $d != 0 && $d != 6){ //check to see if day is weekend or non-school day echo $r; //display the rotation number $r =$r++ % 6 + 1; //increment the number if it is 6, return it to 1 } else { echo '<font color="#ff0000">NONE</Font>'; //when there is no rotation day, display this }}?>[/code] Link to comment https://forums.phpfreaks.com/topic/27448-understanding-modulus/#findComment-125669 Share on other sites More sharing options...
Barand Posted November 17, 2006 Share Posted November 17, 2006 The actual code I used was[code]$rota = $count++ % 6 + 1;[/code]$count++ merely increments $count by 1 each time it is called.$count % 6 gives the remainder when $count is divided by six. This will be a number from 0..5.So add the 1 to give values from 1..6 instead of 0..5 Link to comment https://forums.phpfreaks.com/topic/27448-understanding-modulus/#findComment-125867 Share on other sites More sharing options...
mrplatts Posted November 17, 2006 Author Share Posted November 17, 2006 Ok, I played with the math & fully understand how you used % to solve my problem. Thanks so much Link to comment https://forums.phpfreaks.com/topic/27448-understanding-modulus/#findComment-125885 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.