Home > Article > Backend Development > 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 "非整数"; }
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 "非整数"; }
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 '不是整数'; }
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!