Home > Article > Backend Development > PHP determines whether it is an integer
PHP method to determine whether a variable is an integer
Method 1:
<?php $num=12; //返回right //$num=12.1 返回false if(is_int($num)){ echo "right"; }else{ echo "false"; } ?>
The is_int() method is used here to determine whether the incoming parameter is an integer (int), instead of determining whether it is an integer, which is slightly limited.
Method 2:
<?php $num=12; if(floor($num)==$num){ echo "right"; }else{ echo "false"; } ?>
The floor() method rounds the incoming parameters. Compare the rounded or rounded value with the original value. If they are equal, they are integers; if they are not equal, they are not integers.
Recommended tutorial: PHP video tutorial
The above is the detailed content of PHP determines whether it is an integer. For more information, please follow other related articles on the PHP Chinese website!