PHP ctype_alpha() function with example

PHP ctype_alpha() work: Here, we will find out about the ctype_alpha() work with the example in PHP.

PHP ctype_alpha() work

ctype_alpha() work is a character type (CType) work in PHP, it is utilized to check whether a given string contains just letter sets or not.

It returns genuine – if the string contains just letters in order, else it returns false.

Syntax:

    ctype_alpha(string) : bool

Example:

    Input: "Hello"
    Output: true
    Input: "Hello123"
    Output: false
    Input: "abc@123&a"
    Output: false

PHP Code:

<?php
    $str = "Hello";
    if(ctype_alpha($str))
        echo ("$str contains only alphabets.\n");
    else
        echo ("$str does not contain only alphabets.\n");

    $str = "Hello123";
    if(ctype_alpha($str))
        echo ("$str contains only alphabets.\n");
    else
        echo ("$str does not contain only alphabets.\n");

    $str = "Hello world";
    if(ctype_alpha($str))
        echo ("$str contains only alphabets.\n");
    else
        echo ("$str does not contain only alphabets.\n");

    $str = "abc@123&a";
    if(ctype_alpha($str))
        echo ("$str contains only alphabets.\n");
    else
        echo ("$str does not contain only alphabets.\n");
?>

Output:

Hello contains only alphabets.
Hello123 does not contain only alphabets.
Hello world does not contain only alphabets.
abc@123&a does not contain only alphabets.

Leave a Comment

error: Alert: Content is protected!!