PHP determines if the variable is a digital is_numeric ()

Source: Internet
Author: User

is_numeric-detect if a variable is a numeric or numeric character

<?PHP$tests=Array(    "1380", "1e4", "not numeric",Array(), 9.1);foreach($tests  as $element) {    if(Is_numeric($element)) {        Echo"‘{$element} ' is numeric ',Php_eol; } Else {        Echo"‘{$element} ' is not numeric ',Php_eol; }}?>

Program Run Result:

' + ' is numeric' 1380 ' is numeric' 1e4 ' are numeric' not numeric ' are not numeric' Array ' c4>is isn't numeric ' 9.1 ' is numeric

The string 1e4 is also judged to be a number.
The Is_numeric function does not only support 10 binary numbers, but also 16 binary type numbers. So in the use of the verification of pure natural numbers such as QQ number string, to match intval () integer function.

<? PHP     $id = 0xff33669f;     if (is_numeric($id))      Echo $id, ' Meet the requirements. ‘; // output 4281558687 meets the requirements.     Else     echo$id, ' does not meet the requirements. ';? >

If you need to determine an integer, you can use the Is_int () function to avoid the occurrence of some strings or legitimate numbers.
Is_numeric can determine whether a variable is a numeric or numeric string, but its scope is too wide. Integers, decimals, exponential representations, and 16 decimal values are judged. It is a bit inappropriate to use it when judging the ID. Today, a new decision function is found: ctype_digit, which can only determine integers, which is better than is_numeric. Other ctype_xdigit determine the 16 binary integer, ctype_alpha determine the letter and so on functions.



PHP determines if the variable is a digital is_numeric ()

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.