PrimitiveType

PHP: check whether an integer is odd or even

<?php // One way is to check the result of dividing by 2 using the modulo operator, which shows the remainder after integer division. // When dividing an even number by 2, there will be no remainder. if ($num % 2 == 0) { // even } else { // odd } // Another way is to use bit checking with the bitwise 'and' operator. // This checks that the right-most bit in a binary number equals 1, and if it does, the number is odd. if ($num & 1) { // odd } else { // even }

See also:
JavaScript: check whether an integer is odd or even