How to determine the number of PHP (numeric string does not count)

Source: Internet
Author: User
To see this problem, it should be very simple at first, and there are more ways than one.

Online check, most of the people are using Is_numeric method, but also often follow a sentence, do not think too complex, balabala ...

I did the next test, which is not necessarily correct, is_numeric in the manual is: the detection variable is a numeric or numeric string, a numeric string.

$a = "000022153.26"; Echo is_numeric ($a)? "Y": "N";

The results show Y

Obviously this is not the answer I want, according to some people on the internet, I think of another method, since Is_numeric is a numeric or numeric string, then the string is excluded from the line.

if (! is_string ($a)) {    if (is_numeric ($a)) {        echo "Y";        Exit ();}    } echo "N";

Get the result is correct, N then see a person hint, use Is_nan, I tried the next, the last screen printing N, I began to quite excited, thought to, directly sent the article, in fact, it is wrong, it still put a $ A when float to parse, Is_nan In the manual meaning is to determine whether a number is a non-numeric value, is Nan, not a numbers, when $ A is a string type, such as "a", will be directly error, so using Is_nan is not able to determine whether a variable is a string

echo  Is_nan ($a)? "Y": "N"

And after reading the handbook, we found the easiest way to find it.

$a = "0002133.226", if (Is_int ($a) | | is_float ($a)) {    echo "Y";      } else{echo "N";}

The corresponding GetType ($a) is then compared with "integer" and "Double", if the match is the number, but this efficiency is not high, because it is a string comparison, it is relatively slow

I'm very sorry for those who read the article before, if they are misleading.

No Kuibu, not even thousands of miles.

  • Related Article

    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.