Solution for : What is the simplest most basic way to find out if a number/variable is odd or even in PHP? Is it something to do with mod?
You were right in thinking mod was a good place to start. Here is an expression which will return true if $number is even, false if odd:
$number % 2 == 0
Example:
$number = 20;
if ($number % 2 == 0) {
print "I am even";
}
Output:
I am even
($number & 1 == 0) should be used as it evaluates more efficiently than ($number % 2 == 0)
ReplyDelete