$var="abcdef"; // This is alphabetic
//$var="abc=123"; // This is NOT alphabetic
//$var="123"; // This is NOT alphabetic
//$var='xyz12'; // This is NOT alphabetic
//$var='123.56'; // This is NOT alphabetic
//$var=123; // This is NOT alphabetic
if(ctype_alpha($var)){
echo " This is alphabetic ";
}else{
echo " This is NOT alphabetic ";
}
Syntax
bool ctype_alpha(string $input_string)
Returns True or False
if(ctype_alpha($_POST['var'])){
echo " This is alphabetic ";
}else{ echo "this is not alphabetic";}
$ar=array('abc','as12',123,'ab#1','@12');
foreach ($ar as $var){
if(ctype_alpha($var)){
echo " $var is alphabetic ";
}else{
echo " $var is NOT alphabetic ";
}
echo "<BR>";
}
output
abc is alphabetic
as12 is NOT alphabetic
123 is NOT alphabetic
ab#1 is NOT alphabetic
@12 is NOT alphabetic
$username = "JohnDoe";
if (ctype_alpha($username)) {
echo "Valid username";
} else {
echo "Invalid username";
}
$str = "Hello World";
if (ctype_alpha($str)) {
echo "Valid string";
} else {
echo "Invalid string"; // Output: Invalid string
}
$str = "JohnDoe";
if (preg_match('/^[a-zA-Z]+$/', $str)) {
echo "Valid alphabetic string";
} else {
echo "Invalid string";
}
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.