PHP Two ways to judge whether a variable is an integer _php tutorial

Source: Internet
Author: User
Here are the students to introduce two about the PHP judgment variable is a whole number of two methods, I hope this article will help you.


Method One: Can be used four or five into the number, and then compared with the original number, such as floor (3.1) The result should be 3, at this time obviously 3!=3.1, or with ceil () function also line, this can also determine whether it is an integer.

Method two: Using PHP's own function is_int () can easily determine whether the number is an integer.


To illustrate:


$a = 3.3;


Method One

The code is as follows Copy Code

if (floor ($a) = = $a) {

echo "$a is an integer!";

}else{

echo "$a not an integer!";

}

Method Two,

The code is as follows Copy Code

if (Is_int ($a)) {

echo "$a is an integer!";

}else{

echo "$a not an integer!";

}


Note:is_int () and floor check the type of the variable, not the contents of the variable, when judging the string, you can use the following to replace:

The code is as follows Copy Code

function Str_is_int ($STR)
{
return 0 = = = strcmp ($str, (int) $str);
}

http://www.bkjia.com/PHPjc/632620.html www.bkjia.com true http://www.bkjia.com/PHPjc/632620.html techarticle Here are the students to introduce two about the PHP judgment variable is a whole number of two methods, I hope this article will help you. Method One: You can use four or five into the number, ...

  • 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.