PHP Check Odd or Even Number Tutorial | Guide
Below is an example code written in PHP to determine whether a number is odd or even.
<?php
$num = 10; // 要判断的数字
if ($num % 2 == 0) {
echo "偶数";
} else {
echo "奇数";
}
?>
In the example above, we first define a variable $num to store the number to be checked. We then use the modulo operator % to check if $num is divisible by 2.
If $num can be divided by 2 with a remainder of 0, it is an even number, and we output “even.” Otherwise, it is an odd number, and we output “odd.”