An Introduction to Ctype Functions

The Ctype extension provides a set of functions that are used to verify whether the characters in a string are of the correct type. In this article we’ll take a look at the syntax used by the character type functions, see what specific functions exist, and how they are used to perform validation.

The extension is enabled by default if you’re running PHP 4.2 or above. If you cannot stand the thought of this extension running with your installation for some crazy reason, then you can use the --disable-ctype compile switch to turn it off.

If you have a background in C, then you’re probably already familiar with the character type functions because that is where they come from (don’t forget that PHP is actually written in C). But if you’re into Python, then it’s only fair to point out that the PHP Ctype functions have absolutely nothing to do with the Python’s ctypes library. It’s just one of those tragic and totally unavoidable naming similarities.

So How Does it Work?

It’s all very simple. As I noted before, the functions check a string value to see if the characters are within a given range or that every character is of the appropriate type. For example, you can use these functions to see if a string contains only uppercase characters, or if it’s numeric, or if it consists of hex characters, or one of the other dozen or so options available.

You should be diligent in making sure you always pass in a string. Sure, you can pass in integers, but you’re setting yourself up for trouble as the PHP manual notes on every function’s page:

If an integer between -128 and 255 inclusive is provided, it is interpreted as the ASCII value of a single character (negative values have 256 added in order to allow characters in the Extended ASCII range). Any other integer is interpreted as a string containing the decimal digits of the integer.

This example from the ctype_digit() page illustrates the effect of this:

<?php
$numeric_string = "42";
$integer        = 42;

ctype_digit($numeric_string); // true
ctype_digit($integer);        // false (ASCII 42 is '*')

is_numeric($numeric_string);  // true
is_numeric($integer);         // true

If we look at the code above, the first line evaluates as true. The second statement, however, is false. It is true that 42 is an integer, but the ctype statement evaluates it as a single ASCII character which in this case is an asterisk.

Ctype functions are not the only way to validate data, of course. You can also use the is_numeric() function, depending on your needs. It treats a numeric as just that, a number, and returns true value as shown below:

<?php
is_numeric($numeric_string);  // true
is_numeric($integer);         // true

There are also other is_* functions, including is_float(), is_integer(), etc.

Why are we talking about is_* functions here? Just to remind you there is more than one way to skin a cat. Actually, in today’s age I’m probably not supposed to say that. It’s just an expression. Don’t skin your cat and then tell everyone it was my idea. It’s just that there are multiple ways of doing things.

What Functions are Available

I’ve hinted that a wide range of checking is available, but what exactly are the functions available? What kind of checking is available? The list is below.

  • ctype_alnum() – check for alphanumeric characters (A – Z, upper or lower case, 0-9, no special characters, punctuation, or other freaks).
  • ctype_alpha() – check for alphabetical characters (A-Z, upper or lower case).
  • ctype_cntrl() – check for control characters (things like n, etc.).
  • ctype_digit() – check for numeric characters (0-9, no decimal point, comma, etc.).
  • ctype_graph() – check for visually printable characters (not control characters or space).
  • ctype_lower() – check for lowercase characters (a-z lower case only, no numbers).
  • ctype_print() – check for printable characters including control characters and space.
  • ctype_punct() – check for punctuation type characters (no numbers, letters, or space). Generally these include many of the “swear word” characters that we often call “special” characters. @&!#
  • ctype_space() – check for whitespace characters (includes blanks and any control character that leaves a space on the page, such as “Narrow No-Break Space” and the “Mongolian Vowel Separator”).
  • ctype_upper() – check for uppercase characters (A-Z upper case only, no numbers or special characters).
  • ctype_xdigit() – check for hex characters.

How to Use Them

Using the Ctype functions is pretty easy. You just set it up, generally, in an if-statement, sometimes embedded in a loop if you’re testing a number of values from in an array, and then check if the result of the function call is true or false. True means that every character in that string is the type of character called for by that specific function.

Here’s an example:

<?php
if (ctype_alnum($string)) {
    echo "This string totally works";
}
else {
    echo "And this one not so much";
}

If the value of $string is “Azxc1234″ then you’ll see the message that it works. If $string is “123#Axy” then it won’t because # is not an alphanumeric character.

Note that if you pass in an empty string, the functions will false in PHP 5.1 and above, but true if you are on an earlier version (just another reason to upgrade now!).

And also remember to make sure the input to a Ctype function is a string! When in doubt, there’s no harm in casting it.

<?php
$integer = 42;

ctype_digit($integer);         // false
ctype_digit((string)$integer); // true

Conclusion

And that’s all there is to it! The functions should inherently be part of your PHP installation (if not then you definitely need to upgrade or stop setting up weird PHP set ups). And they are simple to use so long as you only input strings.

So where would you use them? Well, anytime you need to bring a string in from a form you could use them to test the validity of the data you are dealing with. Really, the sky’s the limit. (Fortunately, I don’t have to take a public or private stand on what you use them for; I just say what the functions are and how to use them).

Image via Fotolia

Win an Annual Membership to Learnable,

SitePoint's Learning Platform

No Reader comments

Comments on this post are closed.