ctype_digit
ctype_digit(mixed $text): bool
Checks whether every character of the string is a decimal digit 0-9 (narrow ASCII semantics); entry point of the Ctype character-checking family.
C C23
#include <ctype.h>
#include <stdio.h>
static int all_digits(const char *s) {
for (; *s != '\0'; s++) {
if (!isdigit((unsigned char)*s)) return 0;
}
return 1;
}
int main(void) {
printf("%d\n", all_digits("123"));
printf("%d\n", all_digits("12.3"));
/* isdigit returns nonzero-true: compare against 0 explicitly;
the (unsigned char) cast avoids UB for negative char values */
printf("%d\n", isdigit('7') != 0);
return 0;
}
1 0 1
C++ C++26
#include <cctype>
#include <iostream>
#include <string>
// isdigit returns nonzero-true: compare against 0 explicitly;
// the cast avoids UB for negative char values
static bool allDigits(const std::string& s) {
for (char ch : s) {
if (!std::isdigit(static_cast<unsigned char>(ch))) return false;
}
return true;
}
int main() {
std::cout << std::boolalpha;
std::cout << allDigits("123") << "\n";
std::cout << allDigits("12.3") << "\n";
std::cout << (std::isdigit('7') != 0) << "\n";
return 0;
}
true false true
C# v11.0.100
// char.IsDigit is Unicode-wide: full-width digits count too
static bool AllDigits(string s)
{
foreach (char c in s)
{
if (!char.IsDigit(c)) return false;
}
return true;
}
Console.WriteLine(AllDigits("123"));
Console.WriteLine(AllDigits("12.3"));
Console.WriteLine(AllDigits("123")); // true - Unicode-wide semantics
Console.WriteLine(double.TryParse("12.3", out _)); // numeric-string side
True False True True
Go v1.27.4
package main
import (
"fmt"
"strconv"
"unicode"
)
func main() {
allDigits := func(s string) bool {
for _, r := range s {
if !unicode.IsDigit(r) {
return false
}
}
return true
}
fmt.Println(allDigits("123"))
fmt.Println(allDigits("12.3"))
fmt.Println(allDigits("123")) // unicode.IsDigit is Unicode-wide: true
_, err := strconv.ParseFloat("12.3", 64)
fmt.Println(err == nil)
}
true false true true
Java v27.0.1
// Character.isDigit is Unicode-wide like Python/Go: full-width digits count
class Main {
static boolean allDigits(String s) {
for (int i = 0; i < s.length(); i++) {
if (!Character.isDigit(s.charAt(i))) return false;
}
return true;
}
public static void main(String[] args) {
System.out.println(allDigits("123"));
System.out.println(allDigits("12.3"));
System.out.println(allDigits("123")); // true - Unicode-wide
try {
Double.parseDouble("12.3");
System.out.println(true);
} catch (NumberFormatException e) {
System.out.println(false);
}
}
}
true false true true
JavaScript v26.13.1
// /^\d+$/ without the u flag matches ASCII 0-9 only - the narrow
// semantics matching PHP ctype_digit
console.log(/^\d+$/.test("123"));
console.log(/^\d+$/.test("12.3"));
console.log(/^\d+$/.test("123")); // false - full-width is not \d
// value-level numeric check (the is_numeric side)
console.log(Number.isFinite(Number("12.3")));
console.log(Number.isInteger("123")); // false - strings are not integers
true false false true false
PHP v8.6.0
// ctype_digit: every char is an ASCII decimal digit 0-9
var_dump(ctype_digit("123"));
var_dump(ctype_digit("12.3")); // '.' is not a digit char
var_dump(ctype_digit("123")); // full-width digits are multi-byte, not ASCII
// vs is_numeric: numeric-string semantics (sign, dot, exponent allowed)
var_dump(is_numeric("12.3"));
var_dump(is_numeric(123));
bool(true) bool(false) bool(false) bool(true) bool(true)
Python v3.15.2
# str.isdigit has Unicode semantics: full-width digits count as digits!
# This differs from PHP ctype_digit (narrow ASCII)
print("123".isdigit())
print("12.3".isdigit())
print("123".isdigit()) # True - teaching point
# numeric-string semantics (the is_numeric side): try parsing
def is_numeric_string(s):
try:
float(s)
return True
except ValueError:
return False
print(is_numeric_string("12.3"))
print(is_numeric_string("123"))
True False True True True
Rust v1.99.1
// char::is_ascii_digit is the narrow ASCII check (PHP ctype_digit
// analogue); full-width digits are multi-byte chars, not ASCII
fn all_ascii_digits(s: &str) -> bool {
s.chars().all(|c| c.is_ascii_digit())
}
fn main() {
println!("{}", all_ascii_digits("123"));
println!("{}", all_ascii_digits("12.3"));
println!("{}", all_ascii_digits("123"));
println!("{}", "12.3".parse::<f64>().is_ok()); // numeric-string side
}
true false false true
TypeScript v26.13.1
// Same runtime regex as JS; TS types move the "123" vs 123 question to
// compile time - a string parameter can never sneak in as a number
function isDigitString(s: string): boolean {
return /^\d+$/.test(s); // narrow ASCII semantics
}
console.log(isDigitString("123"));
console.log(isDigitString("12.3"));
console.log(isDigitString("123"));
console.log(Number.isFinite(Number("12.3")));
true false false true
Language differences
C: isdigit(3) is narrow ASCII (the ancestor of the PHP Ctype family); it returns nonzero-true so compare != 0 explicitly; its argument must be representable as unsigned char or EOF — passing a negative char is classic UB, hence the cast in the example.
C++: std::isdigit is the same as C (narrow ASCII + the same UB trap + cast); there is also a locale-aware overload, but the default locale stays narrow.
C#: char.IsDigit is Unicode-wide (Nd category, full-width digits are true); char.IsAsciiDigit (.NET 7+) is the narrow counterpart.
Go: unicode.IsDigit is Unicode-wide (full-width true); use c >= '0' && c <= '9' or strconv.ParseInt for narrow semantics.
Java: Character.isDigit is likewise Unicode-wide (Nd category) — the classic case of semantics opposite to PHP ctype_digit.
JavaScript: /^\d+$/ (no u flag) is narrow ASCII; \d excludes full-width digits even with the u flag — full-width needs \p{Nd} + u (the wide/narrow switch lives in regex syntax).
PHP: the Ctype family has twelve functions (ctype_alnum/alpha/cntrl/digit/graph/lower/print/punct/space/upper/xdigit), all narrow-ASCII by convention; the core difference from is_numeric: ctype asks "is every char a digit", is_numeric asks "is the whole thing a number" (signs, dots, exponents accepted, plus actual int/float) — they disagree on "12.3" (example).
Python: str.isdigit() is Unicode-wide — '123'.isdigit() is True (opposite of PHP; the biggest teaching point here); isdecimal (strict decimal) is narrower than isdigit, while isnumeric is wider (Chinese numerals like 「三」 count).
Rust: char::is_ascii_digit is narrow ASCII (PHP's counterpart); char::is_numeric is the wide one (Unicode Nd/Nl/No all count — much wider than PHP).
TypeScript: runtime identical to JavaScript (narrow \d); literal type narrowing moves the string-vs-number question to compile time.