How to Check if a Number is Odd or Even in PHP

Here’s a simple trick which allows you to determine whether a specific number is even or odd in PHP.

$num = 5;
if($num % 2){
    echo 'Number is odd!';
    echo 'Number is even!';

As you can see, if the result of $num % 2 is anything other than zero the number will be considered Odd.

Read Also: How to Remove Empty Array Elements in PHP?

Leave a Comment.