Method 1: You can use four homes or five to enter the number and compare it with the original number. for example, the result of floor (31) is 3. Obviously, 3 is displayed! 31, or use the ceil () function to determine whether
Method 1: You can use four homes or five to enter the number and compare it with the original number. for example, the result of floor (3.1) is 3. Obviously, 3! = 3.1, or use the ceil () function to determine whether it is an integer.
Method 2: Use the php built-in function is_int () to easily determine whether the number is an integer.
The code is as follows:
- $ A = 3.3;
- // Method 1
- If (floor ($ a) ==$ ){
- Echo "$ a is an integer! ";
- } Else {
- Echo "$ a is not an integer! ";
- }
- // Method 2
- If (is_int ($ )){
- Echo "$ a is an integer! ";
- } Else {
- Echo "$ a is not an integer! ";
- }
Note:The is_int () and floor checks the type of the variable, instead of the content in the variable. when judging the string, you can use the following code instead:
- Function str_is_int ($ str)
- {
- Return 0 === strcmp ($ str, (int) $ str );
- }