PHP ctype_alnum() function with example

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

PHP ctype_alnum() work

ctype_alnum() work is a character type (CType) work in PHP, it is utilized to check whether a given string contains alphanumeric characters or not.

It returns genuine – if the string contains an alphanumeric value (for example letters in order, digits/number just), else it returns false.

Syntax:

    ctype_alnum(string) : bool

Example:

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

Code for it in PHP:

<?php
    $str = "Hello123";
    if(ctype_alnum($str))
        echo ("$str contains only alphanumeric characters.\n");
    else
        echo ("$str does not contain only alphanumeric characters.\n");

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

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

Output:

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

Leave a Comment

error: Alert: Content is protected!!