PHP ctype_xdigit() function with example

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

PHP ctype_xdigit() work

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

It returns genuine, 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.

Syntax:

    ctype_xdigit(string) : bool

Example:

    Input: "ABCD1209"
    Output: true
    Input: "9009aaff"
    Output: true
    Input: "1234"
    Output: true
    Input: "123G4"
    Output: false

PHP Code:

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

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

Leave a Comment

error: Alert: Content is protected!!