Simon Neaves was close on explaining why his function is perfect choice for testing for an int (as possibly most people would need). He made some errors on his ctype_digit() output though - possibly a typo, or maybe a bug in his version of PHP at the time.

As you can see, the reason why using *just* ctype_digit() may not always work is because it only returns TRUE when given a string as input - given a number value and it returns FALSE (which may be unexpected).

If you find yourself deployed in a 32-bit environment where you are required to deal with numeric confirmation of integers (and integers only) potentially breaching the 32-bit span, you can combine is_int() with is_float() to guarantee a cover of the full, signed 64-bit span:

<?php$small = 2147483647; // will always be true for is_int(), but never for is_float()$big = 9223372036854775807; // will only be true for is_int() in a 64-bit environment

"The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). PHP does not support unsigned integers. Integer size can be determined from PHP_INT_SIZE, maximum value from PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5."

This is particularly important if you are doing validation of large keys or any number larger than 2,000,000,000 (e.g. telephone numbers)

I would like to say that is_int() is pretty helpfull when looking for neat proper ways to check functions that return either integers or booleans (false) on failure (strpos, socket_select, etc.)<?phpfunction mySelect() { global $someSockets;$ret = socket_select($someSockets, $o = array(), $e = array(), 0); if (!$ret) return is_int($ret);/* FURTHER PROCESSING HERE */return true; // Return true if the function proceeded as expected.}?>The point of doing this is that if you put this in a while() loopo, you'll break it when the select fails.<?phpwhile (mySelect());?>

I think the function below is a robust test for integers working on all datatypes. It works by first checking that a number can be evaluated numerically, and then secondly that the integer evaluation matches the original number.

Although this can be inferred from the documentation, beware of numeric strings. I ran into the problem in a MySQL app where I would either SELECT an INT PRIMARY KEY or INSERT a new record and use mysql_insert_id() to get the KEY before continuing onto the new section.

I used is_int() to make sure the subsequent queries wouldn't break when using the key variable. Unfortunately I failed to realize that while mysql_insert_id() returns an int, mysql_result() always returns a string even if you are SELECTing from an INT field.

Spent at least 30 minutes trying to figure out why existing records weren't getting linked, but new records would link fine. I ended up using intval() on mysql_result() to make sure subsequent queries still always work.

Remember that some source of data always give a string data type.That's the case with cookies and $_GET.if you need to check against those values, try to convert them into numbers first.The easiest way of doing so is adding a zero before your var, just like:

To check if a string ($s) is a representation of an integer (including representations is scientific notation and negative numbers), you can use the following test, provided that you don't expect values that are out of bounds for an integer.

is_numeric($s) && floatval($s) == intval(floatval($s))

If the test returns true, the string is a representation of an integer.is_numeric (if it works as intended) protects from strings that are not proper numbers.The comparison filters anything that is non_integer

If, for performance reasons, you want to avoid converting to float twice, it can also be written:is_numeric($s) && ($f = floatval($s)) == intval($f)

If you plan to get values that cannot be representated with an integer and are ready to deal with a float, you can use floor instead of intval, as long as you are ready to deal with floats. Even so, that method will become unreliable when the precision of the float becomes insufficient for getting to the fractional part of the number

Be aware that is_numeric (mentioned in this article as the proper way to validate string numbers) also allows numbers in scientific and hexadecimal annotation. Thus DO NOT USE that function to validate user input that will be used as id number for in a query for example, this could cause mysql errors. Use ctype_digit instead.

<?php// check if input is a valid number// first number must be 1 thru 9, followed by a number 0-9, no decimals// true for "1", "1000"// false for "01", "-1", "1.2"function isInteger($input){ return preg_match('@^[1-9][0-9]*$@',$input) === 1;}?>