Check if a number is odd or even in PHP

by Sam  

We know that any intege that is divisible by 2 is an even number else the number is an odd.

The code below will check if a number is odd or even:


$number = 5; //your input

if( $number % 2)
echo 'A number is even';
echo 'A number is an odd';

In mathematics, % symbol denotes percentage(percent). But in computing, we call it modulo(sometimes called modulus). The modulo operation finds the remainder after division of one number by another.


5 % 2 ; //the answer is 1
6 % 3 ; //returns 0
3 % 2 ; //returns 1

If you have additional information, please don’t forget to comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *

Need of a Website?

Maybe you're planning to build a landing page for your website. You might also have a plan to create a web application. I can offer you with an affordable but with good quality services. Don't hesitate to submit your specifications by clicking the button below.

Get Quote