Content · Programming · PHP

PHP ctype_alnum() Function: Usage and Examples

Learn how PHP ctype_alnum works with practical examples, important behavior, relevant alternatives and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Checks whether every byte is an ASCII letter or digit.

What Is ctype_alnum() in PHP?

Checks whether every byte is an ASCII letter or digit.

Its syntax is:

ctype_alnum(mixed $text): bool

Parameters:

  • $text — Accepted as mixed; required argument.

Return type: bool. An empty string returns false. These functions examine bytes and locale classification, not full Unicode character properties.

Availability: Requires the ctype extension, normally bundled with PHP.

Example 1: Basic Usage

<?php

var_dump(ctype_alnum("A42"));

Output:

bool(true)

An empty string returns false. These functions examine bytes and locale classification, not full Unicode character properties.

Example 2: A Different Input

<?php

var_dump(ctype_alnum("A-2"));

Output:

bool(false)

An empty string returns false. These functions examine bytes and locale classification, not full Unicode character properties.

Practical Example: Applying It in a Program

<?php

$samples = ["A1", "AB", "12", "A-1"]; foreach ($samples as $sample) { echo $sample . ": " . (ctype_alnum($sample) ? "yes" : "no") . PHP_EOL; }

Output:

A1: yes
AB: yes
12: yes
A-1: no

An empty string returns false. These functions examine bytes and locale classification, not full Unicode character properties.

Important Behavior and Edge Cases

  • Key behavior: An empty string returns false. These functions examine bytes and locale classification, not full Unicode character properties.
  • Environment: Requires the ctype extension, normally bundled with PHP.

Comparison With Related Functions

FunctionPurpose
ctype_alnum()Checks whether every byte is an ASCII letter or digit.
preg_match()preg_match() evaluates a regular-expression pattern; ctype_* functions test every byte against a predefined character class.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: An empty string returns falseAn empty string returns false. These functions examine bytes and locale classification, not full Unicode character properties.

Key Takeaways

  • Checks whether every byte is an ASCII letter or digit.
  • An empty string returns false. These functions examine bytes and locale classification, not full Unicode character properties.
  • Requires the ctype extension, normally bundled with PHP.

Summary: An empty string returns false. These functions examine bytes and locale classification, not full Unicode character properties.

Share this resourceSend this page to someone who may find it useful.