PHP | ctype_xdigit() Function

The ctype_xdigit() function in PHP used to check each and every character of string/text are hexadecimal digit or not. It return TRUE if all characters are hexadecimal otherwise return FALSE .

Syntax :

ctype_xdigit(string text)

Parameters Used:



  • text : It is mandatory parameter which specifies the tested string.

Return Values:
It returns TRUE if all characters of the string are hexadecimal otherwise return FALSE.

Examples:

Input  :  ABCDEF0123
Output :  Yes

Input  : GFG2018
Output : No
   
Note :  It checking decimal digit or a character from [A-F, a-f].

Below programs illustrate the ctype_xdigit() function.

Program: 1

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to check given string is 
// Hexadecimal character or not
  
$string = 'ABab012';
  
// Checking above string by using
// of ctype_xdigit() function.
if ( ctype_xdigit($string)) {
          
    // if true then return Yes
    echo "Yes \n";
  
} else {
  
    // if False then return No
    echo "No \n";
}
?>

chevron_right


Output:

Yes

Program: 2
Take one more example ctype_xdigit() function to check how it work when input contains uppercase lowercase and symbol character by using input as array of string.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to check given string is 
//Hexadecimal character or not
  
$strings = array(
    'ABCDEF',
    'abcdef',
    '0123456789X',
    '0123456789',
    'Gg@(*&)',
    'GFG'
);
  
// Checking above given strings 
//by used of ctype_xdigit() function .
foreach ($strings as $test) {
      
    if (ctype_xdigit($test)) {
         echo "Yes \n";
    } else {
         echo "No \n";
    }
}
  
?>

chevron_right


Output:

Yes 
Yes 
No 
Yes 
No 
No

References : http://php.net/manual/en/function.ctype-xdigit.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.