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
- <?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> </td>\n";
-
} else {
-
echo "<td>".$dayArray['mday']." </td>\n";
-
$start += ADAY;
-
}
-
}
-
echo "</tr></table>";
-
?>
The part I'm trying to understand is
- if (($count % 7) == 0) {
-
if ($dayArray['mon'] != $month) {
-
break;
-
} else {
-
echo "</tr><tr>\n";
-
}
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
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.
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
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.
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:
-
$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.
|
|
|
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.
|