438,521 Members | 1,420 Online Need help? Post your question and get tips & solutions from a community of 438,521 IT Pros & Developers. It's quick & easy.

# Having troubles understand Modulus

 Expert 100+ P: 664 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 [PHP]
\$day
".\$dayArray['mday']."
\n"; foreach (\$days as \$day) { echo "\n"; } for (\$count=0; \$count < (6*7); \$count++) { \$dayArray = getdate(\$start); if ((\$count % 7) == 0) { if (\$dayArray['mon'] != \$month) { break; } else { echo "\n"; } } if (\$count < \$firstDayArray['wday'] || \$dayArray['mon'] != \$month) { echo "\n"; } else { echo "\n"; \$start += ADAY; } } echo ""; ?>[/PHP] The part I'm trying to understand is [PHP]if ((\$count % 7) == 0) { if (\$dayArray['mon'] != \$month) { break; } else { echo "
\n"; }[/PHP] 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. Aug 27 '08 #1
4 Replies

 Expert 5K+ P: 6,050 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. Aug 27 '08 #2

 Expert 100+ P: 664 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 Aug 27 '08 #3

 Expert 5K+ P: 6,050 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. Aug 27 '08 #4

 Expert 5K+ P: 5,058 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 \$count = 0; while (\$count < 42)  {   // Do the loop code here     \$count ++; }   The for loop syntax is just a shortcut, to make the code cleaner and easier to read. Aug 28 '08 #5 