Computer >> Computer tutorials >  >> Programming >> PHP

ctype_alnum() function in PHP


The ctype_alnum() function in PHP check for alphanumeric character(s). It returns TRUE if every character in string str is either a letter or a digit, FALSE otherwise.

Syntax

ctype_alnum(str)

Parameters

  • str  − The tested string

Return

The ctype_alnum() function returns TRUE if every character in str is either a letter or a digit, FALSE otherwise.

Example

The following is an example −

<?php
$str = array('demo', 'test#$!test');
foreach ($str as $a) {
   if (ctype_alnum($a)) {
      echo "$a consists of all letters or digits. \n";
   } else {
      echo "$a does not have all letters or digits. \n";
   }
}
?>

Output

demo consists of all letters or digits.
test#$!test does not have all letters or digits.