Monthly Archives: December 2012

Checking and verifying Integers in PHP

If you ever have a need to check your string or variable if it’s an integer you probably going to use one of these functions: is_int(), is_numeric(), intval().

Let’s take a look at each function and make sure we’re familiar with what each one does:

Function is_int() – determines whether the given value is an integer or not. Returns TRUE on success and FALSE on failure.

Function is_numeric() – determines whether the given value is a numeric string or not. Returns TRUE on success and FALSE on failure.

Function intval() – gets the integer value of the variable. So if you have a float number this function will return an integer. Be careful with this integer because using large values may return unpredictable results. On success, this function returns the variable with the integer and zero (0) on failure.

If you’re trying to test your string for an integer value then I suggest using the function is_numeric() first to determine if user input is numeric, then apply is_int() function to make sure the numeric value is an integer.

Does this help? If you have a better solution drop me a line.