What's the %?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

What's the %?

Post 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? :lol:

So what does it do?

-Nay
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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">";
}
... ?>
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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>
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Oh, I finally get it!

Thanks a lot to Volka and my engineer sister. :lol:

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 :D.

-Nay
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

mod

Post by itsmani1 »

hi there.....'

wel the sign is called mod and wot it does is that it returns the remainder
that's all about '%'
Post Reply