Home  >  Article  >  Backend Development  >  PHP determines whether it is an integer

PHP determines whether it is an integer

王林
王林Original
2019-09-25 11:54:088173browse

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!

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