My question is the same as the title, what is the best way to check for an even or odd number?
PHP Modulus operator is: % See this page: http://php.net/manual/en/language.operators.arithmetic.php
<?php // PHP Modulus operator is: % $num = 9; echo $num; echo '<br />'; if(!($num%2)) echo 'even';//can be perfectly divided by 2 if($num%2) echo 'odd'; echo '<hr />'; if(!($num%3)) echo 'by3';//can be perfectly divided by 3 if($num%3) echo 'not by3'; ?>
An alternative would be to use a bitwise operator:
if($num & 1) { // number is odd }