Detailed explanation of the method of integer judgment in PHP (with code)

烟雨青岚
Release: 2023-04-09 07:12:01
forward
2955 people have browsed it

Detailed explanation of the method of integer judgment in PHP (with code)

I have seen these three methods on the Internet. Let’s discuss them with you. If you have any questions, please point them out and let us learn together

1. is_int to judge

if(is_int(2.0)){
    echo "整数";
}else{
    echo "非整数";
}
Copy after login

Output non-integer number

Under normal circumstances, it can be judged, but if the number given is 2.0 like this, this judgment method is There will be problems, so this approach is wrong

2, floor, ceil

$a = 2.0;
if(floor($a)==$a){
    echo "整数";
}else{
    echo "非整数";
}
Copy after login

Output integer

Use such a rounding function, so that This approach is possible

3, regular judgment

code snippet

$reg = '/^[0-9][0-9]*$/'
$goods_number = '0';
if( preg_match($reg,$goods_number)){
  echo '是整数';
}else{
  echo '不是整数';
}
Copy after login

output integer

The above is my own summary There are several methods. The second and third ones can be tested by yourself. If you find any problems, you can remind me^_^

Then I found several such PHP judgment functions through the manual. Such as ctype_digit, is_numeric (this can determine hexadecimal numbers). Friends who are interested can test and study it by themselves

Thank you all for reading, I hope you will benefit a lot.

This article is reproduced from: https://blog.csdn.net/u013757199/article/details/51577379

Recommended tutorial: "php tutorial"

The above is the detailed content of Detailed explanation of the method of integer judgment in PHP (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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