PHP ctype_xdigit() Function (With Examples)

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

PHP ctype_xdigit() function

The ctype_xdigit() function is a character type (CType) function in PHP, it is used to check whether a given string contains hexadecimal digits or not.

Syntax

The syntax of the ctype_xdigit() function:

ctype_xdigit(string) : bool

Parameters

The parameters of the ctype_xdigit() function:

  • string: An input string.

Return Value

The return type of this method is bool, it returns true, if all characters of the given strings are hexadecimal digits – which are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, A, B, C, D, E, F, a, b, c, d, e, f. Else it returns false.

Sample Input/Output

Input: "ABCD1209"
Output: true

Input: "9009aaff"
Output: true

Input: "1234"
Output: true

Input: "123G4"
Output: false

PHP ctype_xdigit() Function Example

<?php
    $str = "ABCD1209";
    if(ctype_xdigit($str))
        echo ("$str contains all hexadecimal digits.\n");
    else
        echo ("$str does not contain all hexadecimal digits.\n");

    $str = "9009aaff";
    if(ctype_xdigit($str))
        echo ("$str contains all hexadecimal digits.\n");
    else
        echo ("$str does not contain all hexadecimal digits.\n");

    $str = "1234";
    if(ctype_xdigit($str))
        echo ("$str contains all hexadecimal digits.\n");
    else
        echo ("$str does not contain all hexadecimal digits.\n");

    $str = "123G4"; //Here, "G" is not an hexadecimal digit
    if(ctype_xdigit($str))
        echo ("$str contains all hexadecimal digits.\n");
    else
        echo ("$str does not contain all hexadecimal digits.\n");	
?>

Output

The output of the above example is:

ABCD1209 contains all hexadecimal digits.
9009aaff contains all hexadecimal digits.
1234 contains all hexadecimal digits.
123G4 does not contain all hexadecimal digits.

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.