Home > php教程 > PHP源码 > body text

php判断是否是整数的两种方法

PHP中文网
Release: 2016-05-20 12:56:32
Original
2221 people have browsed it

php判断一个数字是否是整数,面试的时候遇到的。我写了两种方法,不知道还有没有额。


方法一:利用php自带的函数 is_int() 可以轻松判断出该数字是否是整数。


方法二:可以用四舍或者五入该数字,再与原数比较,比如floor(3.1)结果应该是3,此时显然3!=3.1,或者用ceil()函数也行,这样也能判断出是否为整数。


举例说明:


$a = 3.3;

//方法一

if(is_int($a)){

echo "$a 是整数!";

}else{

echo "$a 不是整数!";

}

//方法二

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

echo "$a 是整数!";

}else{

echo "$a 不是整数!";

}


Related labels:
php
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!