You may use ctype_alpha to check for alphabetic character(s).
Similarly, you may use ctype_digit to check for numeric character(s).
is_numeric
— Finds whether a variable is a number or a numeric string
is_numeric()
example:
<?php
$tests = array(
"42",
0b10100111001,
"not numeric",
array(),
9.1
);
foreach ($tests as $element) {
if (is_numeric($element)) {
echo "'{$element}' is numeric", PHP_EOL;
} else {
echo "'{$element}' is NOT numeric", PHP_EOL;
}
}
?>
The above example will output:
'42' is numeric
'1337' is numeric
'not numeric' is NOT numeric
'Array' is NOT numeric
'9.1' is numeric
Where ctype_digit()
and is_numeric()
differ?
Example comparing strings with integers:
<?php
$numeric_string = '42';
$integer = 42;
ctype_digit($numeric_string); // true
ctype_digit($integer); // false (ASCII 42 is the * character)
is_numeric($numeric_string); // true
is_numeric($integer); // true
?>