How to use ctype_digit() function to check if all characters in a string are numerical (1..9 digits) in PHP

1 Answer

0 votes
$arr = array('13.5', "3.14", 'abcXYZ', 'AbCd1fgH7', 'xyz@!$wsp', '123');
    
foreach ($arr as $s) 
{
    if (ctype_digit($s)) 
        echo "$s is only digits <br />";
    else
        echo "$s is NOT only digits <br />";
}
    
/*
run:
  
13.5 is NOT only digits
3.14 is NOT only digits
abcXYZ is NOT only digits
AbCd1fgH7 is NOT only digits
xyz@!$wsp is NOT only digits
123 is only digits 
     
*/

 



answered Mar 18, 2016 by avibootz
...