Are there any potential pitfalls when using the modulus operator (%) to check for even or odd numbers in PHP?

When using the modulus operator (%) to check for even or odd numbers in PHP, a potential pitfall is that the modulus operator can return unexpected results for negative numbers. To solve this issue, you can use the abs() function to get the absolute value of the number before applying the modulus operator.

$num = -5;

if (abs($num) % 2 == 0) {
    echo $num . " is even.";
} else {
    echo $num . " is odd.";
}