English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The ctype_digit() function checks whether all characters in the string are numbers.
ctype_digit(\$text);
It checks whether all characters in the provided string are numbers. It only checks1 ... 9
Number | Parameters and Description |
---|---|
1 | text (required) The string to be tested |
If each character in the text is a decimal digit, it returns TRUE, otherwise it returns FALSE.
Detect elements in an array, whether the elements are all numbers
<?php \$strings = array('122.50',1004', foo!#$bar'); foreach (\$strings as \$test) { if (ctype_digit(\$test)) { echo "\$test all characters are numbers\n"; }else { echo "\$test contains non-numeric characters\n"; } } ?>Test and see‹/›
Output result:
122.50 contains non-numeric characters 1004 All characters are numbers foo!#$bar contains non-numeric characters