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

PHP code to check whether a number is odd or even

WBOY
Release: 2016-07-25 08:57:10
Original
3791 people have browsed it
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 '偶数';
}
?>
Copy after login

Call example:

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

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


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template