note 31066 added to language.expressions
| From: | Ruben at rack1 dot php dot net | Date: | Tue, 08 Apr 2003 21:03:48 +0000 |
| Subject: | note 31066 added to language.expressions | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-46376@lists.php.net to get a copy of this message | ||
In case you want to check whether an INT is even or uneven, you could use the % operator. In the
example below it has been put into an if-statement.
<?php
//Sets the variable a
$a=2;
//Start checking
if (($a%2)==0) {
print "This is an even number"; //TRUE
} else {
print "This is an uneven number"; //FALSE
}
?>
How does it work?
$a%2: constantly substract 2 from int a.
if put in an if-statement like above, it keeps on substracting 2 from int a, until value is exactly
0 or lower than 2 and higher than 0.
Another example.
<?php
$a=9;
$b=8;
$c=7;
$outcome_A = $a % 3;
$outcome_B = $b % 3;
$outcome_C = $c % 3;
print $outcome_A; // result: 0
print $outcome_B; // result: 2
print $outcome_C; // result: 1
?>
I find this function useful for webdesign purposes.
For instance to colour the background of every even entry of database output.
--
http://www.php.net/manual/en/language.expressions.php
http://master.php.net/manage/user-notes.php?action=edit+31066
http://master.php.net/manage/user-notes.php?action=delete+31066
http://master.php.net/manage/user-notes.php?action=reject+31066