note 31066 deleted from language.expressions by victor
| From: | victor@php.net | Date: | Sat, 27 Mar 2004 21:57:41 +0000 |
| Subject: | note 31066 deleted from language.expressions by victor | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-67266@lists.php.net to get a copy of this message | ||
Note Submitter: Ruben Bonke
----
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.