ctype_xdigit to check hexadecimal characters

$var="10af4f4"; // All are hexadecimal chars or digits
$var="10af4g4"; // All or some are not hexadecimal chars
echo " All are hexadecimal chars or digits ";
echo "All or some are not hexadecimal chars ";
bool ctype_xdigit ( string $input_string )
$input_string : String to be checked.

ctype_xdigit() is a PHP filter function checks if all chars in the $input_string are hexadecimal or not. Any thing other than this if present then it return FALSE.

Hexadecimal is positional numeral system with base of 16. The chars it used are from 0 to 9 and from A to F ( lower or upper case ) .

You can change the value of the variable $var in above script and see the result. Decimal points are not allowed ( False ) in ctype_xdigit.

We can check the form data input by users directly by using ctype_xdigit function. Here is one example.

echo " All are hexadecimal chars or digits ";
echo "All or some are not hexadecimal chars or digits";

Checking array elements

foreach ($ar as $var){
	echo " <b>$var</b>  is hexadecimal number ";
	echo " <b>$var</b> is NOT hexadecimal number ";
echo "<BR>";
abc  is hexadecimal number
12ffff  is hexadecimal number
123 is NOT hexadecimal number
FF123  is hexadecimal number 
@12ff is NOT hexadecimal number

Removing blank space before using

It is better to use string function trim to remove blank space present at starting and ending of the variable before using it. Users some time add blank space and this goes wrongly as not alphanumeric variable. Here is the modified script to remove blank space.
$var=trim($var); // remove space at starting and ending of variable. 
echo " all are hexadecimal chars or digits ";
}else{ echo "All or some are not hexadecimal chars or digits";}
echo "<br><br>";// 
echo " Inside if <br>";
For easy understanding here is the form to submit data to above script
<form method=post action='ctype_alnck.php'>
<input type=text name=var>
<input type=submit value=submit>
Here if you want to check only numeric numbers then better to use is_numeric() function.
ctype() ctype_alpha(): alphabetic characters only ctype_alnum(): alphanumeric characters only

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net


    Post your comments , suggestion , error , requirements etc here

    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer