the ctype_* family
ctype_alnum(mixed $text): bool — ctype_alpha(mixed $text): bool — ctype_lower(mixed $text): bool — ctype_upper(mixed $text): bool — ctype_space(mixed $text): bool — ctype_xdigit(mixed $text): bool — ctype_graph(mixed $text): bool — ctype_print(mixed $text): bool — ctype_punct(mixed $text): bool
Aggregate of the other nine ctype functions: alnum/alpha/lower/upper/space/xdigit/graph/print/punct, all narrow-ASCII; compared with each language's ASCII-narrow and Unicode-wide schools (ctype_digit has its own entry).
C C23
#include <ctype.h>
#include <stdio.h>
#include <string.h>
static int all_chars(const char *s, int (*pred)(int)) {
for (size_t i = 0; s[i] != '\0'; i++) {
if (pred((unsigned char)s[i]) == 0) return 0; /* cast: negative char is UB */
}
return 1;
}
int main(void) {
printf("%d\n", all_chars("abc123", isalnum) != 0);
printf("%d\n", all_chars("abc", isalpha) != 0);
printf("%d\n", all_chars("ABC", isupper) != 0);
printf("%d\n", all_chars("abc", islower) != 0);
printf("%d\n", all_chars(" \t\n", isspace) != 0);
printf("%d\n", all_chars("0f9A", isxdigit) != 0);
printf("%d\n", all_chars("a,b", ispunct) != 0);
printf("%d\n", all_chars("a b", isgraph) != 0);
printf("%d\n", all_chars("a b", isprint) != 0);
return 0;
}
1 1 1 1 1 1 0 0 1
C++ C++26
#include <cctype>
#include <cstdio>
/* std::is* keeps the same narrow ASCII semantics as PHP ctype_* */
template <typename F>
static bool allChars(const char *s, F pred) {
for (size_t i = 0; s[i] != '\0'; i++) {
if (pred(static_cast<unsigned char>(s[i])) == 0) return false;
}
return true;
}
int main() {
std::printf("%d\n", allChars("abc123", [](unsigned char c) { return std::isalnum(c) != 0; }));
std::printf("%d\n", allChars("abc", [](unsigned char c) { return std::isalpha(c) != 0; }));
std::printf("%d\n", allChars("ABC", [](unsigned char c) { return std::isupper(c) != 0; }));
std::printf("%d\n", allChars("abc", [](unsigned char c) { return std::islower(c) != 0; }));
std::printf("%d\n", allChars(" \t\n", [](unsigned char c) { return std::isspace(c) != 0; }));
std::printf("%d\n", allChars("0f9A", [](unsigned char c) { return std::isxdigit(c) != 0; }));
std::printf("%d\n", allChars("a,b", [](unsigned char c) { return std::ispunct(c) != 0; }));
std::printf("%d\n", allChars("a b", [](unsigned char c) { return std::isgraph(c) != 0; }));
std::printf("%d\n", allChars("a b", [](unsigned char c) { return std::isprint(c) != 0; }));
return 0;
}
1 1 1 1 1 1 0 0 1
C# v11.0.100
// char.IsLetterOrDigit et al. are Unicode-wide; the narrow ASCII ctype
// semantics are explicit range checks (some helpers like IsAsciiLetterOrDigit
// exist since .NET 7, but ranges keep the whole family uniform)
static bool All(string s, Func<char, bool> p) => s.All(p);
Console.WriteLine(All("abc123", c => c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'));
Console.WriteLine(All("abc", c => c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'));
Console.WriteLine(All("ABC", c => c >= 'A' && c <= 'Z'));
Console.WriteLine(All("abc", c => c >= 'a' && c <= 'z'));
Console.WriteLine(All(" \t\n", c => c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r'));
Console.WriteLine(All("0f9A", c => c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F'));
Console.WriteLine(All("a,b", c => c >= '!' && c <= '/' || c >= ':' && c <= '@' || c >= '[' && c <= '`' || c >= '{' && c <= '~'));
Console.WriteLine(All("a b", c => c >= '!' && c <= '~')); // printable except space
Console.WriteLine(All("a b", c => c >= ' ' && c <= '~')); // printable incl. space
True True True True True True False False True
Go v1.27.4
package main
import "fmt"
// Go's unicode.Is* are Unicode-wide; the narrow ctype semantics are
// hand-written ASCII range checks (this is what PHP Ctype does in C)
func alnum(c rune) bool { return c < 0x80 && (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9') }
func alpha(c rune) bool { return c < 0x80 && (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') }
func upper(c rune) bool { return c >= 'A' && c <= 'Z' }
func lower(c rune) bool { return c >= 'a' && c <= 'z' }
func space(c rune) bool { return c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r' }
func xdigit(c rune) bool { return c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F' }
func punct(c rune) bool {
return c < 0x80 && (c >= '!' && c <= '/' || c >= ':' && c <= '@' || c >= '[' && c <= '`' || c >= '{' && c <= '~')
}
func graph(c rune) bool { return c >= '!' && c <= '~' }
func print(c rune) bool { return c >= ' ' && c <= '~' }
func all(s string, pred func(rune) bool) bool {
for _, c := range s {
if !pred(c) {
return false
}
}
return true
}
func main() {
fmt.Println(all("abc123", alnum))
fmt.Println(all("abc", alpha))
fmt.Println(all("ABC", upper))
fmt.Println(all("abc", lower))
fmt.Println(all(" \t\n", space))
fmt.Println(all("0f9A", xdigit))
fmt.Println(all("a,b", punct))
fmt.Println(all("a b", graph))
fmt.Println(all("a b", print))
}
true true true true true true false false true
Java v27.0.1
import java.util.function.IntPredicate;
public class Main {
// Character.isLetterOrDigit et al. are Unicode-wide; the narrow ASCII
// ctype semantics are hand-written range checks
static boolean all(String s, IntPredicate p) {
return s.chars().allMatch(p);
}
static boolean ascii(int c) { return c < 128; }
public static void main(String[] args) {
System.out.println(all("abc123", c -> ascii(c) && (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9')));
System.out.println(all("abc", c -> ascii(c) && (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')));
System.out.println(all("ABC", c -> c >= 'A' && c <= 'Z'));
System.out.println(all("abc", c -> c >= 'a' && c <= 'z'));
System.out.println(all(" \t\n", c -> c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r'));
System.out.println(all("0f9A", c -> c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F'));
System.out.println(all("a,b", c -> ascii(c) && (c >= '!' && c <= '/' || c >= ':' && c <= '@' || c >= '[' && c <= '`' || c >= '{' && c <= '~')));
System.out.println(all("a b", c -> c >= '!' && c <= '~'));
System.out.println(all("a b", c -> c >= ' ' && c <= '~'));
}
}
true true true true true true false false true
JavaScript v26.13.1
// The narrow ASCII regexes are the direct ctype counterparts
const t = (re, s) => re.test(s);
console.log(t(/^[A-Za-z0-9]+$/, "abc123"));
console.log(t(/^[A-Za-z]+$/, "abc"));
console.log(t(/^[A-Z]+$/, "ABC"));
console.log(t(/^[a-z]+$/, "abc"));
console.log(t(/^[\t\n\f\r ]+$/, " \t\n"));
console.log(t(/^[0-9A-Fa-f]+$/, "0f9A"));
console.log(t(/^[!-/:-@[-`{-~]+$/, "a,b")); // punctuation only: 'a' fails
console.log(t(/^[!-~]+$/, "a b")); // graph: space fails
console.log(t(/^[ -~]+$/, "a b")); // print: space counts
true true true true true true false false true
PHP v8.6.0
// the ctype_* family: every char must satisfy the predicate (narrow ASCII)
var_dump(ctype_alnum("abc123")); // letters or digits
var_dump(ctype_alpha("abc")); // letters only
var_dump(ctype_upper("ABC")); // uppercase only
var_dump(ctype_lower("abc")); // lowercase only
var_dump(ctype_space(" \t\n")); // whitespace chars
var_dump(ctype_xdigit("0f9A")); // hex digits
var_dump(ctype_punct("a,b")); // punctuation only: 'a' fails
var_dump(ctype_graph("a b")); // printable except space: ' ' fails
var_dump(ctype_print("a b")); // printable including space
bool(true) bool(true) bool(true) bool(true) bool(true) bool(true) bool(false) bool(false) bool(true)
Python v3.15.2
import re
# Python's str.is* are Unicode-WIDE (full-width/CJK count); ctype's narrow
# ASCII semantics need an anchored ASCII regex - both schools shown
print("abc123".isalnum()) # wide
print("abc".isalpha()) # wide
print("ABC".isupper()) # wide
print("abc".islower()) # wide
print(" \t\n".isspace()) # wide
print(all(c in "0123456789abcdefABCDEF" for c in "0f9A")) # str has no isxdigit
print(bool(re.fullmatch(r"[!-/:-@\[-`{-~]+", "a,b"))) # narrow punct: no str method
print(bool(re.fullmatch(r"[!-~]+", "a b"))) # narrow graph: space fails
print(bool(re.fullmatch(r"[ -~]+", "a b"))) # narrow print
True True True True True True False False True
Rust v1.99.1
fn all_chars(s: &str, pred: fn(char) -> bool) -> bool {
s.chars().all(pred)
}
fn main() {
// char::is_ascii_* is the one-to-one narrow-ASCII counterpart of ctype_*
println!("{}", all_chars("abc123", |c| c.is_ascii_alphanumeric()));
println!("{}", all_chars("abc", |c| c.is_ascii_alphabetic()));
println!("{}", all_chars("ABC", |c| c.is_ascii_uppercase()));
println!("{}", all_chars("abc", |c| c.is_ascii_lowercase()));
println!("{}", all_chars(" \t\n", |c| c.is_ascii_whitespace()));
println!("{}", all_chars("0f9A", |c| c.is_ascii_hexdigit()));
println!("{}", all_chars("a,b", |c| c.is_ascii_punctuation()));
println!("{}", all_chars("a b", |c| c.is_ascii_graphic())); // graphic excludes space
println!("{}", all_chars("a b", |c| c.is_ascii_graphic() || c == ' '));
}
true true true true true true false false true
TypeScript v26.13.1
// Same runtime regexes as JS, with a typed helper
const t = (re: RegExp, s: string): boolean => re.test(s);
console.log(t(/^[A-Za-z0-9]+$/, "abc123"));
console.log(t(/^[A-Za-z]+$/, "abc"));
console.log(t(/^[A-Z]+$/, "ABC"));
console.log(t(/^[a-z]+$/, "abc"));
console.log(t(/^[\t\n\f\r ]+$/, " \t\n"));
console.log(t(/^[0-9A-Fa-f]+$/, "0f9A"));
console.log(t(/^[!-/:-@[-`{-~]+$/, "a,b"));
console.log(t(/^[!-~]+$/, "a b"));
console.log(t(/^[ -~]+$/, "a b"));
true true true true true true false false true
Language differences
C: the ancestor of the ctype family — isalnum/isalpha/isupper/islower/isspace/isxdigit/ispunct/isgraph/isprint; narrow ASCII semantics; they return nonzero-true, so compare != 0 explicitly; the argument must be representable as unsigned char (a negative char is classic UB — hence the cast in the example).
C++: std::is* is the same as C (narrow ASCII + UB trap + cast); C code migrates almost verbatim.
C#: the default char.IsLetterOrDigit et al. are Unicode-wide; the narrow ASCII semantics are hand-written range checks (.NET 7+ offers partial helpers like char.IsAsciiLetterOrDigit/IsAsciiHexDigit, but there are no counterparts for upper/lower/space/punct, so the example uses ranges uniformly).
Go: unicode.Is* are all wide; the narrow ctype semantics are hand-written ASCII range checks — the punct/graph/print range tables in the example are exactly what the PHP Ctype extension does in C.
Java: Character.isLetterOrDigit et al. are Unicode-wide (Nd/Nl all count); the narrow ASCII ranges are hand-written (example) — the wide-vs-narrow contrast is most visible against PHP.
JavaScript: there is no str.is*; narrow ASCII uses anchored regexes (^[A-Za-z0-9]+$ etc.), wide semantics use \p{...} with the u flag — the wide/narrow switch lives in regex syntax.
PHP: the ctype family has 11 functions (ctype_alnum/alpha/cntrl/digit/graph/lower/print/punct/space/upper/xdigit), all narrow ASCII by convention (any non-ASCII byte is false); ctype_digit has its own entry and this one aggregates the rest. The two schools: PHP/C/C++/Rust ASCII family vs the Python/Java/Go/C# default Unicode-wide family (full-width letters are alnum under wide semantics).
Python: str.is* are Unicode-wide; str has no isxdigit/isgraph/isprint counterparts (xdigit via string.hexdigits membership, graph/print via ASCII-range regexes); for PHP's narrow semantics, anchor an ASCII regex.
Rust: the char::is_ascii_* family (is_ascii_alphanumeric/uppercase/lowercase/whitespace/hexdigit/punctuation/graphic) is the one-to-one narrow-ASCII native counterpart of PHP; wide semantics use char::is_* (Unicode) instead.
TypeScript: same runtime as the JS regexes; type annotations do not affect matching semantics.