Home » PHP » PHP Ctype functions

PHP ctype_xdigit() function with example

PHP ctype_xdigit() function: Here, we are going to learn about the ctype_xdigit() function with example in PHP.
Submitted by IncludeHelp, on February 04, 2019

PHP ctype_xdigit() function

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.

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.

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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.