ctype_digit(): to check numeric characters

bool ctype_digit ( string $input_string )
$input_string : String to be checked.

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

This is a good function to check data coming from insecure sources like user input data. Before storing or processing such data we can use this function check to match our requirement.

Now let us try with some examples.
echo ctype_digit('37'); // Output is 1 
echo "<br>";
echo ctype_digit(37); 
echo "<br>";
echo ctype_digit('2b45');
echo "<br>";
echo ctype_digit('abcd');
echo "<HR>";
ctype_digit(37); output is False as it is taken as ASCII 37, which is %

Example 2

$str = array('434.34', '4003', 'ab!34',67);
foreach ($str as $val) {
if (ctype_digit($val)) {
 echo "<p class='text-success'>The string $val consists of all digits.</p><br>";
} else {
 echo "<p class='text-danger'>The string $val does not consist of one Or  all digits.</p><br>";
Output is here

The string 434.34 does not consist of one Or all digits.

The string 4003 consists of all digits.

The string ab!34 does not consist of one Or all digits.

The string 67 does not consist of one Or all digits.

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

For easy understanding here is the form to submit data to above script


Output of HTML forms.

Output of all html forms are in string format. Hence if you enter number 35 in the above text box , it will be considered as a string 35 and hence output is equal to True. But if you check the below code the output will be equal to False as it will be considered as ASCII 35 which is equal to #.
echo ctype_digit(35);


Post your comments , suggestion , error , requirements etc here .

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