Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between is_int() and ctype_digit()?

People also ask

What is Ctype_digit?

A ctype_digit() function in PHP used to check each and every character of text are numeric or not. It returns TRUE if all characters of the string are numeric otherwise return FALSE. Syntax : ctype_digit(string text) Parameter Used: The ctype_digit() function in PHP accepts only one parameter.

Is numeric in PHP?

Definition and UsageThe is_numeric() function checks whether a variable is a number or a numeric string. This function returns true (1) if the variable is a number or a numeric string, otherwise it returns false/nothing.


is_int() returns true if the argument is an integer type, ctype_digit() takes a string argument and returns true if all the characters in the string are digits.

Example:

┌──────────┬───────────┬────────────────┐
│          │  is_int:  │  ctype_digit:  │
├──────────┼───────────┼────────────────┤
│ 123      │  true     │  false         │
├──────────┼───────────┼────────────────┤
│ 12.3     │  false    │  false         │
├──────────┼───────────┼────────────────┤
│ "123"    │  false    │  true          │
├──────────┼───────────┼────────────────┤
│ "12.3"   │  false    │  false         │
├──────────┼───────────┼────────────────┤
│ "-1"     │  false    │  false         │
├──────────┼───────────┼────────────────┤
│ -1       │  true     │  false         │
└──────────┴───────────┴────────────────┘

There is also is_numeric which returns true if passed in value can be parsed as number.

enter image description here

If I try to compare performance of functions on PHP 5.5.30 here are the results:

enter image description here

This is the code I used for benchmark

// print table cell and highlight if lowest value is used
function wr($time1, $time2, $time3, $i) {
    if($i == 1) $time = $time1;
    if($i == 2) $time = $time2;
    if($i == 3) $time = $time3;

    echo('<td>');
    if(min($time1, $time2, $time3) === $time) printf('<b>%.4f</b>', $time);
    else printf('%.4f', $time);
    echo('</td>');
}


$test_cases = array( 123, 12.3, '123', true);
$tests = 1000000;
$result = true;  // Used just to make sure cycles won't get optimized out
echo('<table>'.PHP_EOL);
echo('<tr><td>&nbsp;</td><th>is_int</th><th>ctype_digit</th><th>is_numeric</th></tr>');
foreach($test_cases as $case) {
    echo('<tr><th>'.gettype($case).'</th>');

    $time = microtime(true);
    for($i = 0; $i < $tests; $i++) {
        $result |= is_int((int)rand());
    }
    $time1 = microtime(true)-$time;

    $time = microtime(true);
    for($i = 0; $i < $tests; $i++) {
        $result |= ctype_digit((int)rand());
    }
    $time2 = microtime(true)-$time;

    $time = microtime(true);
    for($i = 0; $i < $tests; $i++) {
        $result |= is_numeric((int)rand());
    }
    $time3 = microtime(true)-$time;

    wr($time1, $time2, $time3, 1);
    wr($time1, $time2, $time3, 2);
    wr($time1, $time2, $time3, 3);
    echo('</tr>'.PHP_EOL);
}

echo('</table>');

exit();

The last thing you should be worrying about is how fast one of these is. There is no way that checking a string for being an integer is going to be a bottleneck in your code.


ctype not always return false on integer type.

foreach(range(-1000 , 1000)as $num){
    if(ctype_digit($num)){
        echo $num . ", ";
    }    
}

ctype_digit return true for the following integer type number.

-78,-77,-71,48,49,50,51,52,53,54,55,56,57,178,179,185, 256,257,258,259,260,261,262,263,264,265,266,267,268,269,270 to 1000

the base practice is to case every number to string e.q. strval($num) or (String) $num in this case negative value (-78) will always return false.


is_int will return you true on int type value between -2147483647 to 2147483647. any value exceed that number will return you false presuming it is running on 32bits system. on 64bits it can go up to range of -9223372036854775807 to 9223372036854775807


in term of performance personally very hard to say. ctype_digit maybe faster than is_int but if you have to cast every value to string performance is reduced overall.


Well it's very interesting :) Here is all story:

is_numeric: — Finds whether a variable is a number or a numeric string, No matter value is negative or Boolean or String or any type of number, if value is purely number it will return 'true' else 'false'.

Remember: No Character Only Number any type :)


is_init— Find whether the type of a variable is integer, if value is purely integer then it will return 'true' else 'false'.

Remember: No Character, Double or Negative, Only Integer


in_integer— Alias of is_int()


intval:— Get the integer value of a variable, it's take all kind of value and returns only Integer value, if values are negative then returns '-Integer' value. No matter values are Integer, Float, Negative or 'NumberString' or 'NumberStringCharacter'. It's subtract the Integer values from string "If String Starts with Number".

  • NumberString = A Number value in String Format
  • NumberStringCharacter = A String Start with Number

Remember: You will get Integer value from Number, Float, Negative or String which is starts with Number.


ctype_digit— Check for numeric character(s), If a Whole Number supplied in String Format you will get 'true' else 'false'. It will work with only StringNumber, No Float, No Negative only Whole Number as String.

Remember: Whole Number as String, No Negative Number, No Float Number, No Number Type, No Character, Only Number as String.

Birds Eye View:

enter image description here

Thanks to http://php.net/