PHP Reference Β· String Functions
PHP ctype functions
Reference for the PHP ctype functions: ctype_alpha, ctype_digit, ctype_alnum, ctype_upper, ctype_space and more, the empty-string rule and why integers are deprecated.
The ctype functions check whether every character of a string belongs to a certain class: letters, digits, whitespace, punctuation and so on. They are fast, they need no regular expression, and they return a plain true or false.
All of them follow the same rules. They look at bytes, interpreted in the current locale (in practice, ASCII). They return false for an empty string. Since PHP 8.1 passing anything other than a string is deprecated, so convert integers first. Because a string like "-5" contains a minus sign, ctype_digit("-5") is false.
<?php var_dump(ctype_digit("12345")); // bool(true) var_dump(ctype_digit("12.5")); // bool(false) var_dump(ctype_alpha("")); // bool(false)
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input Β· what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input Β· what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
ctype_alnum(mixed $text): bool ctype_alpha(mixed $text): bool ctype_cntrl(mixed $text): bool ctype_digit(mixed $text): bool ctype_graph(mixed $text): bool ctype_lower(mixed $text): bool ctype_print(mixed $text): bool ctype_punct(mixed $text): bool ctype_space(mixed $text): bool ctype_upper(mixed $text): bool ctype_xdigit(mixed $text): bool
| Function | True when every character is... |
|---|---|
ctype_alpha |
a letter (a-z, A-Z in the default locale). |
ctype_digit |
a decimal digit 0-9. |
ctype_alnum |
a letter or a digit. |
ctype_upper / ctype_lower |
an upper-case / lower-case letter. |
ctype_xdigit |
a hexadecimal digit 0-9, a-f, A-F. |
ctype_space |
whitespace: space, tab, newline, carriage return, vertical tab, form feed. |
ctype_punct |
a printable character that is neither alphanumeric nor a space. |
ctype_print |
printable, including the space. |
ctype_graph |
printable and visible, so no space. |
ctype_cntrl |
a control character. |
Return value: true if the string is not empty and every byte is in the class, otherwise false.
Usage notes
- Empty string is
false, which is the opposite of what a "for all characters" test normally gives. Check emptiness separately if it matters. - Pass strings. Since PHP 8.1, giving an int or other non-string is deprecated and produces a deprecation notice. In older versions an int between -128 and 255 was treated as an ASCII code, which led to bugs such as
ctype_digit(5)beingfalse. - Not Unicode-aware.
ctype_alpha("naΓ―ve")isfalsein the default locale becauseΓ―is two bytes above 127. For international text usepreg_match('/^\p{L}+$/u', $s)ormb_functions. - Digits only means digits only. No sign, no decimal point, no spaces. Use
is_numeric()for numeric strings andfilter_var($v, FILTER_VALIDATE_INT)for integers with a sign. - Locale. Changing the locale with
setlocale()can change which bytes count as letters. Avoid relying on that in new code. - Speed. These checks beat
preg_match()for simple classes, but readability matters more than the difference. - Validation is not sanitization.
ctype_alnumtells you that input is safe to use as an identifier; it does not clean anything.
Related
is_numeric(),preg_match(),mb_strlen(),str_contains()- Lessons: Form validation, Regular expressions, Multibyte strings