PHP | ctype_digit() (Checks for numeric value)
Last Updated :
11 Jul, 2025
Improve
A ctype_digit() function in PHP used to check each and every character of text are numeric or not. It returns TRUE if all characters of the string are numeric otherwise return FALSE.
Syntax :
PHP
PHP
PHP
ctype_digit(string text)Parameter Used: The ctype_digit() function in PHP accepts only one parameter.
- text : Its mandatory parameter which specifies the tested string.
- Gives Expected result when passing string not gives desired output when passing an integer.
- The function returns true on empty string previous versions of PHP 5.1.0
Input :789495001 Output : Yes Explanation : All digits, return True Input : 789.0877 Output : No Explanation: String contains floating point, return false.Below programs illustrate the ctype_digit() function. Program: 1 Checking ctype_digit() function for a single string which contains all digits.
<?php
// PHP program to check given string is
// control character
$string = '123456789';
// Checking above given string
// by using of ctype_digit() function.
if ( ctype_digit($string)) {
// if true then return Yes
echo "Yes\n";
} else {
// if False then return No
echo "No\n";
}
?>
Output:
Program: 2
Drive a code ctype_digit() function where input will be array of string which contains special characters, integers, strings.
Yes
<?php
// PHP program to check is given string
// contains all digits using ctype_digit
$strings = array(
'Geeks-2018',
'geek@yahoo.com',
'10.99999Fe',
'12345',
'geeksforgeeks.org'
);
// Checking above given strings
//by used of ctype_digit() function .
foreach ($strings as $testcase) {
if (ctype_digit($testcase)) {
// if true then return Yes
echo "Yes\n";
} else {
// if False then return No
echo "No\n";
}
}
?>
Output:
ctype_digit() vs is_int()
ctype_digit() is basically for string type and is_int() for integer types. ctype_digit() traverses through all characters and checks if every character is digit or not. For example,
No No No Yes No
<?php
// PHP code to demonstrate difference between
// ctype_digit() and is_int().
$x = "-123";
if (ctype_digit($x))
echo "Yes\n";
else
echo "No\n";
$x = -123;
if (is_int($x))
echo "Yes";
else
echo "No";
?>
Output:
References :https://www.php.net/manual/en/function.ctype-digit.php
No Yes
Article Tags :