How to Determine if a Number is Odd or Even in PHP?


You want to write a program that will check if a number is even or odd.


We know that a number is even if it is completely divide by 2.

The modulus arithmetic operator can tell you the reminder when you divide a number by another number. To test an even number, we’ll divide it by 2. If it is completely divided by 2 (or in other words, it is an even number), it will returns 0. Otherwise, it is an odd number. See it in code-

$number = 229;
echo ($number%2 == 0) ? $number." is an even number." : $number." is an odd number.";

[wpdm_file id=55]

229 is an odd number.

How it works:
After dividing 229 by 2, the reminder is 1, so it is an odd number.