PHP ctype_alpha() Function (With Examples)

In this tutorial, we will learn about the PHP ctype_alpha() function with its usage, syntax, parameters, return value, and examples. By IncludeHelp Last updated : December 31, 2023

PHP ctype_alpha() function

The ctype_alpha() function is a character type (CType) function in PHP, it is used to check whether a given string contains only alphabets or not.

Syntax

The syntax of the ctype_alpha() function:

ctype_alpha(string) : bool

Parameters

The parameters of the ctype_alpha() function:

  • string: An input string.

Return Value

The return type of this method is bool, it returns true – if the string contains only alphabets, else it returns false.

Sample Input/Output

Input: "Hello"
Output: true

Input: "Hello123"
Output: false

Input: "abc@123&a"
Output: false

PHP ctype_alpha() Function Example

<?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

The output of the above example is:

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.

To understand the above example, you should have the basic knowledge of the following PHP topics:

All PHP Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.