Differences between is_numeric and ctype_digit in PHP

Source: Internet
Author: User
In PHP, the two functions is_numeric and ctype_digit both check whether the string is a number, but there is also a difference: is_numeric: checks whether it is a numeric string, can be a negative number and a decimal number

Ctype_digit: checks whether all characters in a string are numbers. negative and decimal digits cannot be detected.

Note that the parameter must be a string. if it is not a string, 0/FASLE is returned.

The following is a test example:

The code is as follows:


$ A = 0001111222;
Var_dump ($ );
Var_dump (is_numeric ($ a); // true
Var_dump (ctype_digit ($ a); // true
$ A = 0.1;
Var_dump ($ );
Var_dump (is_numeric ($ a); // true
Var_dump (ctype_digit ($ a); // false

$ A =-1;
Var_dump ($ );
Var_dump (is_numeric ($ a); // true
Var_dump (ctype_digit ($ a); // false

$ A =;
Var_dump ($ );
Var_dump (is_numeric ($ a); // false
Var_dump (ctype_digit ($ a); // false

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.