Home >Backend Development >PHP Tutorial >Two ways to determine whether a variable is an integer in PHP_PHP Tutorial

Two ways to determine whether a variable is an integer in PHP_PHP Tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 10:50:401490browse

The following will introduce to you two methods of using PHP to determine whether a variable is an integer. I hope this article will be helpful to you.


Method 1: You can round or round the number, and then compare it with the original number. For example, the result of floor(3.1) should be 3. In this case, obviously 3!=3.1, or you can use the ceil() function, which can also be judged. Whether the output is an integer.

Method 2: Use the function is_int() that comes with PHP to easily determine whether the number is an integer.


Example:


$a = 3.3;


//Method 1

}
The code is as follows
 代码如下 复制代码

if(floor($a)==$a){

 echo "$a 是整数!";

}else{

 echo "$a 不是整数!";

}

Copy code

 代码如下 复制代码

if(is_int($a)){

 echo "$a 是整数!";

}else{

 echo "$a 不是整数!";

}

if(floor($a)==$a){


echo "$a is an integer!"; }else{

 代码如下 复制代码

function   str_is_int($str)  
{
        return   0   ===   strcmp($str   ,   (int)$str);
}

echo "$a is not an integer!";

The code is as follows Copy code
if(is_int($a)){ echo "$a is an integer!"; }else{ echo "$a is not an integer!"; }
Note: is_int() and floor check the type of the variable, not the content in the variable. When judging a string, you can use the following instead:
The code is as follows Copy code
function str_is_int($str) {               return   0   ===   strcmp($str , (int)$str); } http://www.bkjia.com/PHPjc/632620.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632620.htmlTechArticleThe following will introduce two methods for you to determine whether a variable is an integer in PHP. I hope this article will be helpful to you. Classmates will help. Method 1: You can round or round the number,...
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