ctype_alpha
Checks whether every character in a string is a letter.
ctype_alpha(mixed $text): bool- Added in PHP 4.0.4
Parameters
| Parameter | Type | Description |
|---|---|---|
$text | mixed | The string to test. Every byte must be a letter for the result to be true. An int between -128 and 255 is interpreted as an ASCII codepoint, which is why non-string arguments are deprecated as of PHP 8.1. |
Return value
booltrue if every character in text is a letter in the current locale, false otherwise. An empty string always returns false.
Errors
Never throws. A non-string argument raises an E_DEPRECATED notice as of PHP 8.1; values that are neither string nor int return false.
Changelog
- PHP 8.1Passing a non-string argument is deprecated. Ints between -128 and 255 are still read as ASCII codepoints, so cast to string or use chr() explicitly.
ctype_alpha() reports whether a string consists entirely of letters. It is equivalent to ctype_upper() or ctype_lower() being true for each character, so mixed case such as McDonald passes while McDonald's does not.
When to reach for it
- Checking a two-letter country or currency code contains no digits.
- Deciding whether a token in a parser is a bare word rather than a number or operator.
- Filtering a list down to purely alphabetic entries with
array_filter($list, 'ctype_alpha')— the function name works directly as a callable.
What trips people up
Real-world names are not alphabetic. Spaces, hyphens, apostrophes and accented letters all fail, so using this to validate a person's name will reject Jean-Luc, O'Neil and Zoë. It is a good check for machine-generated identifiers and a bad check for human input.
The locale matters too. Under the default C locale only ASCII A-Z and a-z count; a UTF-8 multi-byte letter is several bytes and none of them is an ASCII letter, so the whole string fails.
Example
When to use it
- Verifying an ISO country code like 'DE' contains only letters before a lookup
- Splitting a version tag such as 'beta2' into its alphabetic and numeric parts
- Filtering a word list down to entries with no digits or punctuation
Frequently asked questions
What is the difference between ctype_alpha and ctype_alnum?
ctype_alpha accepts letters only, while ctype_alnum also accepts digits. So ctype_alpha('abc1') is false but ctype_alnum('abc1') is true.
Can I use ctype_alpha to validate a person's name?
Not reliably. It rejects spaces, hyphens, apostrophes and accented letters, which appear in perfectly valid names, so it will reject Jean-Luc and Zoë. Use a Unicode regex such as /^[\p{L} '-]+$/u instead.
Does ctype_alpha care about uppercase and lowercase?
No, it accepts both. Use ctype_upper or ctype_lower when you need to test the case of the letters.
More examples
Using the name as a callable
Prints an array containing only 'Ada' and 'Turing'. Everything with an apostrophe, a space or a digit is dropped. array_values() re-indexes because array_filter preserves the original keys.
<?php
$names = ['Ada', "O'Neil", 'Jean Luc', 'X4E', 'Turing'];
$alphabetic = array_values(array_filter($names, 'ctype_alpha'));
print_r($alphabetic);Splitting a token into letters and digits
Prints "letters: beta, digits: 12". Testing one character at a time is the standard way to classify a string when the whole-string answer is not enough.
<?php
$token = 'beta12';
$letters = '';
$digits = '';
foreach (str_split($token) as $ch) {
if (ctype_alpha($ch)) {
$letters .= $ch;
} elseif (ctype_digit($ch)) {
$digits .= $ch;
}
}
echo "letters: $letters, digits: $digits\n";
Discussion