Get "PHP 8 in a Nuthshell" (Now with PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Checking the type of a string in PHP

If you’re working with PHP, you might have come across a situation where you need to check the type of the string. For instance, you might want to check if the string is a number, alphabetic, alphanumeric, or a binary string.

PHP has this set of functions prefixed with ctype_ which can effortlessly check the type of a string. Let’s take a look at them one by one. By the way, these functions have been available since PHP 4.0.4.

Checking if the string is alphabetic

To check if the string contains only alphabetic characters, you can use the ctype_alpha function like so.

ctype_alpha('Hello World'); // true
ctype_alpha('Hello World 123'); // false

Checking if the string is numeric

To check if the string contains only numeric characters, you can use the ctype_digit function like so.

ctype_digit('123'); // true
ctype_digit('Hello World'); // false

Checking if the string is alphanumeric

To check if the string contains only alphanumeric characters, you can use the ctype_alnum function like so.

ctype_alnum('Hello World 123'); // true
ctype_alnum('Hello World 123!'); // false

Checking if the string is binary

To check if the string contains only binary characters, you can use the ctype_xdigit function like so.

ctype_xdigit('0F'); // true
ctype_xdigit('0FZ'); // false

Checking if the string is whitespace

To check if the string contains only whitespace characters, you can use the ctype_space function like so.

ctype_space("\n\r\t"); // true
ctype_space("Hello World"); // false

Checking if the string is printable

To check if the string contains only printable characters, you can use the ctype_print function like so.

Here, a string is not printable if it contains any control characters like \n, \r, \t, etc.

ctype_print("Hello World"); // true
ctype_print("Hello World\n"); // false

Checking if the string is a control character

To check if the string contains only control characters, you can use the ctype_cntrl function like so.

ctype_cntrl("\n\r\t"); // true
ctype_cntrl("Hello World"); // false

Checking if the string is a graph character

To check if the string contains only graph characters, you can use the ctype_graph function like so.

This means any printable character(s) except space.

ctype_graph("Hello World"); // true
ctype_graph("Hello World\n"); // false

Checking if the string is a lowercase

To check if the string contains only lowercase characters, you can use the ctype_lower function like so.

ctype_lower("hello world"); // true
ctype_lower("Hello World"); // false

Checking if the string is an upper case

To check if the string contains only upper case characters, you can use the ctype_upper function like so.

ctype_upper("HELLO WORLD"); // true
ctype_upper("Hello World"); // false

Checking if the string is a punctuation

To check if the string contains only punctuation characters, you can use the ctype_punct function like so.

ctype_punct("*&$()!"); // true
ctype_punct("Hello World!"); // false
Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?