PHP determines whether it is an integer

王林
Release: 2023-02-24 20:22:01
Original
8174 people have browsed it

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";   
       }
?>
Copy after login

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";   
       }
?>
Copy after login

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!

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template