Home >Backend Development >PHP Problem >How to determine whether a number is an integer in php

How to determine whether a number is an integer in php

青灯夜游
青灯夜游Original
2022-05-31 19:39:555524browse

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.

How to determine whether a number is an integer in php

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$num=3.14;
if(floor($num)===$num){
	echo "是整数";
}else{
	echo "不是整数";
}
?>

How to determine whether a number is an integer in php

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$num=3;
if(is_int($num)){
	echo "是整数";
}else{
	echo "不是整数";
}
?>

How to determine whether a number is an integer in php

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!

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