Jump to content

understanding modulus --


mrplatts

Recommended Posts

Bear with me, I'm learning

I 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, 6

I 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

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]<?php

for ( $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

??? I understand that I transposed the units in the example I gave... 3%7=1

OK -- 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]
<?php
function 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 - 6
if (!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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.