note 70424 added to language.operators.arithmetic
| From: | JonathonReinhart at osu1 dot php dot net | Date: | Mon, 16 Oct 2006 09:11:54 +0000 |
| Subject: | note 70424 added to language.operators.arithmetic | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-118566@lists.php.net to get a copy of this message | ||
A very simple yet maybe not obvious use of the modulus (%) operator is to check if an integer is odd
or even.
<?php
if (($a % 2) == 1)
{ echo "$a is odd." ;}
if (($a % 2) == 0)
{ echo "$a is even." ;}
?>
This is nice when you want to make alternating-color rows on a table, or divs.
<?php
for ($i = 1; $i <= 10; $i++) {
if(($i % 2) == 1) //odd
{echo "<div class=\"dark\">$i</div>";}
else //even
{echo "<div class=\"light\">$i</div>";}
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 64.93.132.54
----
Manual Page -- http://www.php.net/manual/en/language.operators.arithmetic.php
Edit -- https://master.php.net/note/edit/70424
Del: integrated -- https://master.php.net/note/delete/70424/integrated
Del: useless -- https://master.php.net/note/delete/70424/useless
Del: bad code -- https://master.php.net/note/delete/70424/bad+code
Del: spam -- https://master.php.net/note/delete/70424/spam
Del: non-english -- https://master.php.net/note/delete/70424/non-english
Del: in docs -- https://master.php.net/note/delete/70424/in+docs
Del: other reasons-- https://master.php.net/note/delete/70424
Reject -- https://master.php.net/note/reject/70424
Search -- https://master.php.net/manage/user-notes.php