English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP ctype_digit() Function Usage and Example

PHP Ctype Function Manual

The ctype_digit() function checks whether all characters in the string are numbers.

Syntax

ctype_digit(\$text);

Definition and Usage

It checks whether all characters in the provided string are numbers. It only checks1 ... 9

Parameter

NumberParameters and Description
1

text (required)

The string to be tested

Return value

If each character in the text is a decimal digit, it returns TRUE, otherwise it returns FALSE.

Online Example

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

  PHP Ctype Function Manual