Home > Article > Backend Development > PHP code to check whether a number is odd or even
This article introduces a piece of code that uses PHP to determine odd and even numbers for your reference.
This article will share with you a PHP custom function, which is used to detect whether a number is odd or even, and return true or false. The code is very simple and suitable for beginners to refer to. Code: <?php /** * 数字检测奇偶 * by bbs.it-home.org */ $num = 3; /** * * @Check if number is odd or even * * @var $num The number to check * * Return BOOL * */ function checkNum($num){ return ($num%2) ? TRUE : FALSE; } //调用 if(checkNum($num) === TRUE){ echo '奇数'; }else{ echo '偶数'; } ?> Call example: <?php $num=100; if(checkNum($num)=== TRUE;){ echo "$num is even"; } $num=231; if(checkNum($num) === TRUE){ echo "$num is odd"; } |