$var="10af4f4"; // All are hexadecimal chars or digits
$var="10af4g4"; // All or some are not hexadecimal chars
if(ctype_xdigit($var)){
echo " All are hexadecimal chars or digits ";
}else{
echo "All or some are not hexadecimal chars ";
}
Syntax
bool ctype_xdigit ( string $input_string )
$input_string : String to be checked. $input_string
are hexadecimal or not. Any thing other than this if present then it return FALSE.
if(ctype_xdigit($_POST['var'])){
echo " All are hexadecimal chars or digits ";
}else{
echo "All or some are not hexadecimal chars or digits";
}
$ar=array('abc','12ffff',123,'FF123','@12ff');
foreach ($ar as $var){
if(ctype_xdigit($var)){
echo " <b>$var</b> is hexadecimal number ";
}else{
echo " <b>$var</b> is NOT hexadecimal number ";
}
echo "<BR>";
}
Output
abc is hexadecimal number
12ffff is hexadecimal number
123 is NOT hexadecimal number
FF123 is hexadecimal number
@12ff is NOT hexadecimal number
<?Php
$var=$_POST['var'];
$var=trim($var); // remove space at starting and ending of variable.
if(ctype_xdigit($var)){
echo " all are hexadecimal chars or digits ";
}else{ echo "All or some are not hexadecimal chars or digits";}
echo "<br><br>";//
if(!ctype_xdigit($var)){
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>
</form>
Here if you want to check only numeric numbers then better to use is_numeric() function.
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.