Is one more preferred, or performs better over the other?
+7
A:
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
Robert Gamble
2008-10-25 13:34:51
+1
A:
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.
Andy Lester
2008-10-25 13:40:18
+1
A:
If you don't really care if the argument is a int type or a string with numbers, use is_numeric. It will return true for floats also, tho.
gnud
2008-10-26 12:05:02
A:
Little correction here
ctype_digit returns true for "123" AND for 123.
Ramito
From http://uk3.php.net/ctype_digit "Checks if all of the characters in the provided string, text , are numerical." So ctype_digit only works on strings, represented by "123" and not numeric types, represented by 123
timmow
2009-08-26 13:30:31