Page 1 of 1
What's the %?
Posted: Thu Oct 16, 2003 10:13 am
by Nay
I was looking around good old spoono and I've just came across the alternative row coloring tutorial. So I took a look at it. I understood everything except the $i % 2.
Code: Select all
<table width="300" height="400">
<?
// Connect to MySQL
mysql_connect ('localhost', 'username', 'password') ;
mysql_select_db ('dbname');
$result = mysql_query (SELECT * FROM table);
if ($row = mysql_fetch_array($result)) {
//run the for loop
for ($i = 0; i < count($row); $i++) {
// Set Table Row Color
if ($i % 2) {
echo "<tr bgcolor="B0C4DE">";
} else {
echo "<tr bgcolor="FOF8FF">";
}
// Now display our table cell info
}
?>
<td><? echo $row["whatever"]; ?></td>
</tr>
</table>
<? }
?>
Honestly, in all my months of developing PHP, I've never used the % sign nor come across it until now. Strange eih?
So what does it do?
-Nay
Posted: Thu Oct 16, 2003 10:24 am
by volka
it's the modulus operator as described at
http://www.php.net/manual/en/language.o ... hmetic.php
Code: Select all
<?php
for($i=0; $i!=13; $i++)
echo 'the remainder of ', $i, '/4 is ', $i%4, " \n";
?>
Code: Select all
<?php ...
if ($i % 2) {
// $i%2 == 1, $i is odd
echo "<tr bgcolor="B0C4DE">";
} else {
// $i%2 == 0, $i is even
echo "<tr bgcolor="FOF8FF">";
}
... ?>
Posted: Thu Oct 16, 2003 10:43 am
by Nay
I see. I first thought of it as "if $i is divisible by 2" but then no, since this:
Code: Select all
<?php
for($i=0; $i!=13; $i++)
echo 'the remainder of ', $i, '/4 is ', $i%4, " \n<br />";
?>
outputs:
the remainder of 0/4 is 0
the remainder of 1/4 is 1
the remainder of 2/4 is 2
the remainder of 3/4 is 3
the remainder of 4/4 is 0
the remainder of 5/4 is 1
the remainder of 6/4 is 2
the remainder of 7/4 is 3
the remainder of 8/4 is 0
the remainder of 9/4 is 1
the remainder of 10/4 is 2
the remainder of 11/4 is 3
the remainder of 12/4 is 0
The mysterey of 0,1,2,3. I can code PHP but I'm not the best of maths in class - that's another story

.
-Nay
ps: yes, this means I still don't really get it. I'm still reading over the operators in the documentation
Posted: Thu Oct 16, 2003 10:53 am
by volka
hm, try this one
Code: Select all
<pre><?php
$i = 5; // change this if you like
$result = $i/4;
echo $i, ' divided by 4 is: ', $result, "\n";
echo $result, ' four times is: ', $result*4, "\n";
echo $i, ' - ', $result*4, ' is: ', $i-($result*4), "\n";
echo "---\n";
echo "now we only have integers\n";
$result = (int)($i/4);
echo $i, ' divided by 4 is: ', $result, "\n";
echo $result, ' four times is: ', $result*4, "\n";
echo $i, ' - ', $result*4, ' is: ', $i-($result*4), "\n";
echo "---\n";
echo $i, ' modulus 4 is: ', $i%4, "\n";
echo "---\n";
echo '4 is ', $result, ' times in ', $i, '. Remainder: ', $i%4, "\n";
?></pre>
Posted: Fri Oct 17, 2003 7:27 am
by Nay
Oh, I finally get it!
Thanks a lot to Volka and my engineer sister.
I asked my sister if she knows modulus in maths soon after I was tearing my hair off. Well, she graduated top in her maths classes so yeah, I thought I give it a try.
whoohoo, something new in my brain now

.
-Nay
mod
Posted: Fri Oct 17, 2003 8:05 am
by itsmani1
hi there.....'
wel the sign is called mod and wot it does is that it returns the remainder
that's all about '%'