Php can determine whether a variable is an integer.

Source: Internet
Author: User
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:

  1. $ A = 3.3;
  2. // Method 1
  3. If (floor ($ a) ==$ ){
  4. Echo "$ a is an integer! ";
  5. } Else {
  6. Echo "$ a is not an integer! ";
  7. }
  8. // Method 2
  9. If (is_int ($ )){
  10. Echo "$ a is an integer! ";
  11. } Else {
  12. Echo "$ a is not an integer! ";
  13. }

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:

  1. Function str_is_int ($ str)
  2. {
  3. Return 0 === strcmp ($ str, (int) $ str );
  4. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.