Almost every PHP application, be it a Web form or a CLI
program, requires user input. And because users are notoriously fickle, it’s
important to check the data they enter for validity before using it for
anything important—for instance, a calculation or a database entry.

Fortunately, there’s a hidden jewel in the PHP toolkit that
can help with this task. It’s the PHP ctype extension, and it contains functions to test if a
value belongs to a particular character class—that is, whether it contains only
alphabetic characters, only numbers, only printable or special characters, or
combinations thereof.

This document outlines the more useful functions available
in this PHP extension, with explanations and usage examples. (Table A)

Table A

Function

Explanation

Example

ctype_alpha($val)

This function returns true if every character in the input
string is from the set of alphabetic characters.

Use this function for input that must contain alphabetic
characters—for example, personal names.

<?php
// returns true
echo ctype_alpha(“hello”) ? “true” : “false”;

// returns false
echo ctype_alpha(“h3ll0”) ? “true” : “false”;
?>

ctype_digit($val)

This function returns true if every character in the input
string is a number.

Use this function for input that must only contain
numbers—for example, age values or credit card numbers.

Note: Decimal
points in floating-point numbers will cause this function to return false.

<?php
// returns true
echo ctype_digit(“5419520217”) ? “true” : “false”;

// returns false
echo ctype_digit(“64.78”) ? “true” : “false”;
?>

ctype_alnum($val)

This function returns true if every character in the input
string is either a number or an alphabetic character.

Note: White
space or punctuation in the input string will cause this function to return
false.

<?php
// returns true
echo ctype_alnum(“OX14HP”) ? “true” : “false”;

// returns false
echo ctype_alnum(“9 o’clock”) ? “true” : “false”;
?>

ctype_space($val)

This function returns true if every character in the input
string “creates whites pace”—that is, it is a space, tab, line feed or carriage return character.

<?php
// returns true
echo ctype_space(”    “) ? “true” : “false”;

// returns false
echo ctype_space(“a b”) ? “true” : “false”;
?>

ctype_cntrl($val)

This function returns true if every character in the input
string is a control character, such as a tab or carriage return.

Note: White
space in the input string will cause this function to return false.

<?php
// returns true
echo ctype_cntrl(“\t”) ? “true” : “false”;

// returns false
echo ctype_cntrl(”  “) ? “true” : “false”;
?>

ctype_punct($val)

This function returns true if every character in the input
string is a punctuation character—that is, not an alphabetic, numeric,
control or white space character.

<?php
// returns true
echo ctype_punct(“?;*&^%$#”) ? “true” : “false”;

// returns false
echo ctype_punct(“6*5”) ? “true” : “false”;
?>

ctype_print($val)

This function returns true if every character in the input
string is printable—that is, an alphabetic, numeric, punctuation or white
space character. The presence of control characters, such as line feeds or
tabs, will cause this function to return false.

<?php
// returns true
echo ctype_print(“abc&%$245 “) ? “true” : “false”;

// returns false
echo ctype_print(“\r\n\t”) ? “true” : “false”;
?>

ctype_graph($val)

This function returns true if every character in the input
string is printable and not white space—that is, an alphabetic, numeric or
punctuation character. The presence of white space or control characters,
such as line feeds or tabs, will cause this function to return false.

<?php
// returns true
echo ctype_graph(“abc&%$245”) ? “true” : “false”;

// returns false
echo ctype_graph(“abc&%$245 “) ? “true” : “false”;
?>

ctype_upper($val)

and

ctype_lower($val)

These functions return true if every character in the
input string is an alphabetic character and either upper-case or lower-case
respectively.

<?php
// returns true
echo ctype_upper(“ABC”) ? “true” : “false”;

// returns false
echo ctype_lower(“ABC”) ? “true” : “false”;

// returns false
echo ctype_upper(“2”) ? “true” : “false”;
?>