Two methods: 1. Use floor() to round, and then compare it with the original number. The syntax is "floor($n)===$n". If they are equal, it is an integer, otherwise it is not an integer. . 2. Use is_int() to detect, the syntax is "is_int($n)", if it returns TRUE, it is an integer, if it returns FALSE, it is not an integer.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php determines a number Two methods to determine whether it is an integer
Method 1: Use floor() to round, and then compare it with the original number
<?php header('content-type:text/html;charset=utf-8'); $num=3.14; if(floor($num)===$num){ echo "是整数"; }else{ echo "不是整数"; } ?>
Method 2: Use the function is_int() that comes with PHP
The is_int() function is used to detect whether the variable is an integer. TRUE if the specified variable is an integer, FALSE otherwise.
<?php header('content-type:text/html;charset=utf-8'); $num=3; if(is_int($num)){ echo "是整数"; }else{ echo "不是整数"; } ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to determine whether a number is an integer in php. For more information, please follow other related articles on the PHP Chinese website!