[PHP 4, PHP 5, PHP 7, PHP 8]
is_int — Find whether the type of a variable is integer
Description
is_int[mixed $value
]: bool
Note:
To test if a variable is a number or a numeric string [such as form input, which is always a string], you must use is_numeric[].
Parameters
value
The variable being evaluated.
Return Values
Returns
true
if value
is an int, false
otherwise.
Examples
Example #1 is_int[] example
The above example will output:
is_int[23] = bool[true] is_int['23'] = bool[false] is_int[23.5] = bool[false] is_int['23.5'] = bool[false] is_int[NULL] = bool[false] is_int[true] = bool[false] is_int[false] = bool[false]
See Also
- is_bool[] - Finds out whether a variable is a boolean
- is_float[] - Finds whether the type of a variable is float
- is_numeric[] - Finds whether a variable is a number or a numeric string
- is_string[] - Find whether the type of a variable is string
- is_array[] - Finds whether a variable is an array
- is_object[] - Finds whether a variable is an object
Simon Neaves ¶
14 years ago
I've found that both that is_int and ctype_digit don't behave quite as I'd expect, so I made a simple function called isInteger which does. I hope somebody finds it useful.
Robin ¶
12 years ago
Keep in mind that is_int[] operates in signed fashion, not unsigned, and is limited to the word size of the environment php is running in.
In a 32-bit environment:
In a 64-bit environment:
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:
e dot sand at elisand dot com ¶
13 years ago
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.
The correct output for parts of his examples should be:
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].
andre dot roesti at 7flex dot net ¶
12 years ago
With this function you can check if every of multiple variables are int. This is a little more comfortable than writing 'is_int' for every variable you've got.
davide dot renzi at gmail dot com ¶
6 years ago
I've found a faster way of determining an integer.
On my env, this method takes about half the time of using is_int[].
Cast the value then check if it is identical to the original.
petepostma at gmail dot spam dot com ¶
10 years ago
There is a versa to the vice of this int only type check.
is_int[ $integer_type] will only return true, if the TYPE is int, not the value
ctype_digit[ $string_type] will only return true if the TYPE is string, and its value is INT
therefore:
return [ is_int[$value] || ctype_digit[$value] ];
nicolas dot giraud at actiane dot com ¶
10 years ago
Just a shorter way to check if your variable is an int or a string containing a int without others digit than 0 to 9 :