Home  >  Article  >  Backend Development  >  PHP code to check whether a number is odd or even

PHP code to check whether a number is odd or even

WBOY
WBOYOriginal
2016-07-25 08:57:103764browse
This article introduces a piece of code that uses PHP to determine odd and even numbers for your reference.

This article will share with you a PHP custom function, which is used to detect whether a number is odd or even, and return true or false. The code is very simple and suitable for beginners to refer to.

Code:

<?php

/**
* 数字检测奇偶
* by bbs.it-home.org
*/
$num = 3;

/**
*
* @Check if number is odd or even
*
* @var $num The number to check
*
* Return BOOL
*
*/
function checkNum($num){
  return ($num%2) ? TRUE : FALSE;
}

//调用
if(checkNum($num) === TRUE){
  echo '奇数';
}else{
  echo '偶数';
}
?>

Call example:

<?php
$num=100;
if(checkNum($num)=== TRUE;){
  echo "$num is even";
}

$num=231;
if(checkNum($num) === TRUE){
   echo "$num is odd";
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn