<?php
//Check an array of data for alphabets and digits.
$strings = array('AbCd1zyZ9', 'foo!#$bar');
foreach ($strings as $testcase) {
if (ctype_alnum($testcase)) {
echo "The string $testcase consists of all letters or digits.
<br />";
} else {
echo "The string $testcase does not consist of all letters or digits.
<br />";
}
}
?>
<?php
//Check a variable for alphabets and digits.
$testcase = '-123';
if (ctype_alnum($testcase)) {
echo "The string $testcase consists of all letters or digits.
<br />";
} else {
echo "The string $testcase does not consist of all letters or digits.
<br />";
}
?>
<?php
//Check a variable for alphabets.
$testcase = 'ABC';
if (ctype_alpha($testcase)) {
echo "The string $testcase consists of all letters.
<br />";
} else {
echo "The string $testcase does not consist of all letters.
<br />";
}
?>
<?php
//Check a variable for numbers.
$testcase = '123';
if (ctype_digit($testcase)) {
echo "The string $testcase consists of all digits.
<br />";
} else {
echo "The string $testcase does not consist of all digits.
<br />";
}
?>
The string AbCd1zyZ9 consists of all letters or digits.
The string foo!#$bar does not consist of all letters or digits.
The string -123 does not consist of all letters or digits.
The string ABC consists of all letters.
The string 123 consists of all digits.