By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,521 Members | 1,420 Online
Bytes IT Community
+ Ask a Question
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

nomad
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]<?php
$days = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
echo "<TABLE BORDER=1 CELLPADDING=5><tr>\n";
foreach ($days as $day) {
echo "<TD BGCOLOR=\"#CCCCCC\" ALIGN=CENTER><strong>$day</strong></td>\n";
}
for ($count=0; $count < (6*7); $count++) {
$dayArray = getdate($start);
if (($count % 7) == 0) {
if ($dayArray['mon'] != $month) {
break;
} else {
echo "</tr><tr>\n";
}
}
if ($count < $firstDayArray['wday'] || $dayArray['mon'] != $month) {
echo "<td>&nbsp;</td>\n";
} else {
echo "<td>".$dayArray['mday']." &nbsp;&nbsp; </td>\n";
$start += ADAY;
}
}
echo "</tr></table>";
?>[/PHP]


The part I'm trying to understand is
[PHP]if (($count % 7) == 0) {
if ($dayArray['mon'] != $month) {
break;
} else {
echo "</tr><tr>\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
Share this Question
Share on Google+
4 Replies


Markus
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

nomad
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

Markus
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

Atli
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
  1. $count = 0;
  2. while ($count < 42) 
  3. {
  4.   // Do the loop code here
  5.  
  6.   $count ++;
  7. }
  8.  
The for loop syntax is just a shortcut, to make the code cleaner and easier to read.
Aug 28 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.