PHP - Function ctype_digit()



The PHP Character type checking ctype_digit() function is used to check whether the provided string contains only numeric characters. The "Numeric" characters refer to decimal digits from 0 to 9. For example, in the string "1234", all characters are numeric.

This function returns a boolean value true if every character in the string is a decimal digit, and false otherwise. If the provided string is empty (""), the function always returns false.

Syntax

Following is the syntax of the PHP Character type checking ctype_digit() function −

ctype_digit (mixed $text): bool

Parameters

This function accepts the following parameters −

  • text (required) − A string needs to be checked or tested.

Return Value

This function returns 'true', if every character in the text is a decimal digit, otherwise, it returns 'false'.

Example 1

If the provided text contains the decimal digit, the PHP ctype_digit() function will return true

<?php
   $text = "1232";
   echo "The given text: ".$text;
   #using ctype_digit() function
   echo "\nDoes the text contain only decimal digits? ";
   var_dump(ctype_digit($text));
?>

Output

This will produce the following result −

The given text: 1232
Does the text contain only decimal digits? bool(true)

Example 2

If the provided text does not contain the decimal digit, the PHP ctype_digit() function will return false

<?php
   $text = "Tutorialspoint";
   echo "The given text: ".$text;
   #using ctype_digit() function
   echo "\nDoes the text contain only decimal digits? ";
   var_dump(ctype_digit($text));
?>

Output

After executing the above, the following output will be displayed −

The given text: Tutorialspoint
Does the text contain only decimal digits? bool(false)

Example 3

If the provided text or string is empty (""), this function will always return false

<?php
   $text = " ";
   echo "The given text: ".$text;
   #using ctype_digit() function
   echo "\nDoes the text contains only decimal digits? ";
   var_dump(ctype_digit($text));
?>

Output

Once the above program is executed, it will return 'false' −

The given text:
Does the text contain only decimal digits? bool(false)

Example 4

Checking multiple strings (texts).

In the following example, we declare an array containing multiple texts and will check each string to determine whether they are alphanumeric using the ctype_alnum() function −

<?php
   $texts = array('123432', 'hello', '12hi');
   echo "The given Strings are: ";
   foreach($texts as $text){
	   echo $text." ";
   }
   foreach($texts as $text){
	   if(ctype_digit($text)){
		   echo "\nThe string '$text' contains only decimal digits.";
	   }
	   else{
		   echo "\nThe string '$text' does not contain only decimal digits.";
	   }
   }
?>

Output

Following is the output of the above program −

The given Strings are: 123432 hello 12hi
The string '123432' contains only decimal digits.
The string 'hello' does not contain only decimal digits.
The string '12hi' does not contain only decimal digits.
php_function_reference.htm
Advertisements