sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
nomad's Avatar

Having troubles understand Modulus


Question posted by: nomad (Expert) on August 27th, 2008 06:50 PM
If I understand this right modules is used to divide a number and if there is a remainder that remainder is used?

here is part of a code that I'm trying to understand which produce part of a Calendar

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $days = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
  3. echo "<TABLE BORDER=1 CELLPADDING=5><tr>\n";
  4. foreach ($days as $day) {
  5.     echo "<TD BGCOLOR=\"#CCCCCC\" ALIGN=CENTER><strong>$day</strong></td>\n";
  6. }
  7. for ($count=0; $count < (6*7); $count++) {
  8.     $dayArray = getdate($start);
  9.     if (($count % 7) == 0) {
  10.         if ($dayArray['mon'] != $month) {
  11.             break;
  12.         } else {
  13.             echo "</tr><tr>\n";
  14.         }
  15.     }
  16.     if ($count < $firstDayArray['wday'] || $dayArray['mon'] != $month) {
  17.         echo "<td>&nbsp;</td>\n";
  18.     } else {
  19.         echo "<td>".$dayArray['mday']." &nbsp;&nbsp; </td>\n";
  20.         $start += ADAY;
  21.     }
  22. }
  23. echo "</tr></table>";
  24. ?>



The part I'm trying to understand is
Expand|Select|Wrap|Line Numbers
  1. if (($count % 7) == 0) {
  2.         if ($dayArray['mon'] != $month) {
  3.             break;
  4.         } else {
  5.             echo "</tr><tr>\n";
  6.         }


I know its trying to produce rolls for the week, but i just don't get it. I have read how its works several times. Question what is $count % 7) == 0 being divide by were does the variable $count gets it value from?

thanks

nomad

PS if you need more code I can give it out.
4 Answers Posted
Markus's Avatar
Markus August 27th, 2008 07:57 PM
Moderator - 2,727 Posts
#2: Re: Having troubles understand Modulus

It's saying (because '%' is used to find the remainder of a number') if the remainder of $count / 7 is 0 (ie, 7 goes into $count perfectly) then run the code.

Hope this helps.
nomad's Avatar
nomad August 27th, 2008 08:31 PM
Expert - 562 Posts
#3: Re: Having troubles understand Modulus

Quote:
Originally Posted by markusn00b
It's saying (because '%' is used to find the remainder of a number') if the remainder of $count / 7 is 0 (ie, 7 goes into $count perfectly) then run the code.

Hope this helps.


I get that part but still trying to figure out where the number is coming from
for ($count=0; $count < (6*7); $count++) { // ie $count is the variable coming from the user from the pull down menu?

sorry new to this info.

nomad
Markus's Avatar
Markus August 27th, 2008 08:41 PM
Moderator - 2,727 Posts
#4: Re: Having troubles understand Modulus

Quote:
Originally Posted by nomad
I get that part but still trying to figure out where the number is coming from
for ($count=0; $count < (6*7); $count++) { // ie $count is the variable coming from the user from the pull down menu?

sorry new to this info.

nomad


No, count is 0 and then incremented in the for loop. Am I missing your problem? Haha.
Atli's Avatar
Atli August 28th, 2008 09:38 PM
Moderator - 2,760 Posts
#5: Re: Having troubles understand Modulus

Hi.

Check out for loops in the manual.

The $count variable, in your case, is created inside the for loop to keep track of the number of loops that have been executed.

You could re-write that for loop like so, as a while loop:
Expand|Select|Wrap|Line Numbers
  1. $count = 0;
  2. while ($count < 42) 
  3. {
  4.   // Do the loop code here
  5.  
  6.   $count ++;
  7. }

The for loop syntax is just a shortcut, to make the code cleaner and easier to read.
Reply
Not the answer you were looking for? Post your question . . .
197,036 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,036 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top PHP Contributors