PHP - Function ctype_xdigit()



The PHP Character type checking ctype_xdigit() function checks whether every character in the given string represents a hexadecimal digit.

The hexadecimal digits are the characters used in the base-16 numbering system. This system includes the digits 0 to 9, which represent values zero through nine, and the letters A to F (or a to f), which represent values ten through fifteen. Following is the complete set of hexadecimal digits:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9
A (10), B (11), C (12), D (13), E (14), F (15)

This function returns a boolean value true if the given string contains all hexadecimal digits; otherwise, it returns false. If the given string is empty (""), this function always returns "false."

Syntax

Following is the syntax of the PHP Character type checking ctype_xdigit() function −

ctype_xdigit (mixed $text): bool

Parameters

This function accepts the following parameters −

  • text (required) − A string needs to be checked.

Return Value

This function returns "true", if every character in the text is a hexadecimal 'digit'; "false" otherwise.

Example 1

The following program demonstrates the usage of the PHP ctype_xdigit() function −

<?php
   $string = "ABCDEF132";
   echo "The given string is: $string";
   echo "\nDoes the string '$string' consist of all hexadecimal digits?? ";
   #using ctype_xdigit() function
   var_dump(ctype_xdigit($string));
?>

Output

After executing the above program, the following output will be displayed −

The given string is: ABCDEF132
Does the string 'ABCDEF132' consist of all hexadecimal digits?? bool(true)

Example 2

If the given string does not have all characters representing the hexadecimal digit, the PHP ctype_xdigit() function will return false

<?php
   $string = "tutoriasl@331";
   echo "The given string is: $string";
   echo "\nDoes the string '$string' consist of all hexadecimal digits?? ";
   #using ctype_xdigit() function
   var_dump(ctype_xdigit($string));
?>

Output

Following is the output of the above program −

The given string is: tutoriasl@331
Does the string 'tutoriasl@331' consist of all hexadecimal digits?? bool(false)

Example 3

If the given string (text) is empty (""), this function will always return 'false' −

<?php
   $string = "";
   echo "The given string is: $string";
   echo "\nDoes the string '$string' consist of all hexadecimal digits?? ";
   #using ctype_xdigit() function
   var_dump(ctype_xdigit($string));
?>

Output

The above program produces the following output −

The given string is:
Does the string '' consist of all hexadecimal digits?? bool(false)
php_function_reference.htm
Advertisements