PHP - Function ctype_alpha()



The PHP Character type checking ctype_alpha() function is used to check whether all characters in the provided string are alphabetic. An "alphabetic" character refers to letters from A to Z, both uppercase and lowercase. For example, in the string "hello," the characters 'h', 'e', 'l', 'l', and 'o' are all alphabetic characters.

This function returns a boolean value false, if the provided string contains any non-alphabetic characters (such as numbers or punctuation); otherwise, it returns true. If the provided text is empty (""), this function always returns 'false'.

Syntax

Following is the syntax of the PHP Character type checking ctype_alpha() function −

ctype_alpha (mixed $text): bool

Parameters

This function accepts a single parameter, which is described below −

  • text (required) − A string needs to be checked or tested.

Return Value

This function returns 'true' if every character in the text is a letter (alphabetic); otherwise, it returns 'false'.

Example 1

If the given text (string) "contains" only alphabetic (letters), the PHP ctype_alpha() function will return true

<?php
   $text = "Tutorialspoint";
   echo "The given text: ".$text;
   #using ctype_alpha() function
   echo "\nIs the text is alphanumeric? ";
   var_dump(ctype_alpha($text));
?>

Output

Following is the output of the above program −

The given text: Tutorialspoint
Is the text is alphanumeric? bool(true)

Example 2

If the given text (string) does "not contain" only alphabetic (letters), the PHP ctype_alpha() function will return false

<?php
   $text = "Tutorix12";
   echo "The given text: ".$text;
   #using ctype_alpha() function
   echo "\nIs the text is alphanumeric? ";
   var_dump(ctype_alpha($text));
?>

Output

The above program produces the following output −

The given text: Tutorix12
Is the text is alphanumeric? bool(false)

Example 3

If the provided text or string is empty "", this function will always return false

<?php
   $text = " ";
   echo "The given text: ".$text;
   #using ctype_alpha() function
   echo "\nIs the text is alphanumeric? ";
   var_dump(ctype_alpha($text));
?>

Output

After executing the above program, it will return 'false' −

The given text:
Is the text is alphanumeric? bool(false)
php_function_reference.htm
Advertisements