Check If a Number is Odd Or Even In PHP & JavaScript


Odd numbers and even numbers are used in programming languages. To check if a number is even or odd needs brilliant code. In this post, you will be introduced to checkNum() function in both PHP and JavaScript. This functions will return even for even numbers and odd for odd numbers.

Parameters

$num – An integer (PHP)

num   – An integer (JS)

Returns

“odd” for odd number, “even” for even number.

Function

JavaScript

function checkNum(num){

 if(num % 2){

  return “even”;

 }else{

  return “odd”;

 }

}

PHP

function checkNum($num){

 if($num % 2){

  return “even”;

 }else{

  return “odd”;

 }

}