$var='37'; // All are numeric chars
//$var=37; // All or some are not numeric chars
//$var='2b45'; // All or some are not numeric chars
//$var='abcd'; // All or some are not numeric chars
if(ctype_digit($var)){
echo "<b>$var</b> All are numeric chars ";
}else{
echo " <b>$var</b> All or some are not numeric chars";
}
Output37 All are numeric chars
Syntax
bool ctype_digit ( string $input_string )
$input_string : String to be checked. $input_string
are numeric or not. Any thing other than this if present then it return FALSE.
echo ctype_digit('37'); // Output is 1
echo "<br>";
echo ctype_digit(37);
echo "<br>";
echo ctype_digit('2b45');
echo "<br>";
echo ctype_digit('abcd');
echo "<HR>";
ctype_digit(37);
output is False as it is taken as ASCII 37, which is %
$str = array('434.34', '4003', 'ab!34',67);
foreach ($str as $val) {
if (ctype_digit($val)) {
echo "<p class='text-success'>The string $val consists of all digits.</p><br>";
} else {
echo "<p class='text-danger'>The string $val does not consist of one Or all digits.</p><br>";
}
}
Output is here The string 434.34 does not consist of one Or all digits.
The string 4003 consists of all digits.
The string ab!34 does not consist of one Or all digits.
The string 67 does not consist of one Or all digits.
You can change the value of the variable $var and see the result. Decimal points are not allowed ( False ) in ctype_digit.
echo ctype_digit(35);
Check for only alphabetic characters by ctype_alpha()
Check for all lower or upperCheck for at least one lower case or upper case