is_numeric, intval, ctype__digit.. can you rely on them? is_numeric, intval, ctype__digit.. can you rely on them? php php

is_numeric, intval, ctype__digit.. can you rely on them?


One important difference between ctype_digit and is_numerict is negative values and float number.

is_numeric(-10) will return true whereas 'ctype_digit(-10)' will be false

also ctype_digit(12.50) will return false whereasis_numeric(12.50) will be true

So both of them are handy depends on the context of your domain logic.


is_numeric, intval, and ctype_digit all do very different things.

is_numeric will tell you if the contents of the variable are numeric (i.e. true if it is a floating point or integer value).

intval attempts to convert a string of numbers to an integer value

ctype_digit will tell you if a string contains nothing but numeric characters (will perform the same check as you isNum function).

best approach is probably to check if is_numeric is true and then use something along the lines of settype($myvalue, 'integer') or intval($myvalue);


You named two kinds of functions:

A Validator checks if the given value has the given characteristics and returns either true or false.
is_numeric, the ctype_* functions and your isNum function are validating functions as they just tell you if a value is valid or not.

A Filter changes the given value in such way that the new value has the given characteristics and thus will be valid.
intval and the filter_* functions are filtering functions as they always will return valid values that would pass a validator.