PHP ctype_alnum() Function (With Examples)

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

PHP ctype_alnum() Function

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

Syntax

The syntax of the ctype_alnum() function:

ctype_alnum(string) : bool

Parameters

The parameters of the ctype_alnum() function:

  • string: An input string.

Return Value

It returns true – if the string contains alphanumeric value (i.e. alphabets, digits/number only), else it returns false.

Sample Input/Output

Input: "Hello123"
Output: true

Input: "Hello world"
Output: false

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

PHP ctype_alnum() Function Example

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

The output of the above example is:

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

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.