is_int()
The is_int() function is a built-in function in PHP that checks whether a variable is an integer or not. An integer is a data type that represents a whole
Introduction
The is_int() function is a built-in PHP function that reports whether a variable is of the integer type. An integer is a whole number (no decimal part) such as -7, 0, or 42.
The key word is type: is_int() looks at how the value is stored in memory, not at how it looks. The string "42" and the float 42.0 both look like integers to a human, but to PHP they are a string and a float, so is_int() returns false for both. This is what makes the function useful for strict validation, and also what trips people up most often.
is_int() has two identical aliases — is_integer() and is_long() — that you may see in older code. Prefer is_int() in new code.
Syntax
is_int(mixed $value): bool| Parameter | Description |
|---|---|
$value | The variable to test. |
Return value: true if $value is of type int, otherwise false.
Note:
is_int()never converts the value or raises an error — it only inspects the existing type. To convert a value to an integer, useintval()or an(int)cast.
Basic example
Here $var1 holds an integer, so is_int() returns true. $var2 is a string, so it returns false.
We use var_dump() instead of echo on purpose. When you echo a boolean, true prints as "1" and false prints as an empty string — so echo is_int($var2) shows nothing at all, which is easy to misread. var_dump() prints the type and value explicitly, making the result unambiguous.
What counts as an integer (and what doesn't)
The most common surprise is that a number written inside quotes is a string, not an integer:
<?php
var_dump(is_int(42)); // bool(true) — integer literal
var_dump(is_int("42")); // bool(false) — numeric string, not an int
var_dump(is_int(42.0)); // bool(false) — float, even though it has no fraction
var_dump(is_int(0x1A)); // bool(true) — hex literal 26 is still an int
var_dump(is_int(true)); // bool(false) — a bool is not an int
var_dump(is_int(null)); // bool(false)
?>Notice 42.0: it has no fractional part, but it is stored as a float, so is_int() is false. If you want "is this a whole number regardless of type", you need a different check (see below).
Watch out: integer overflow becomes a float
PHP integers have a maximum size (PHP_INT_MAX). When a calculation exceeds it, PHP silently converts the result to a float, and is_int() then returns false:
<?php
var_dump(is_int(PHP_INT_MAX)); // bool(true)
var_dump(is_int(PHP_INT_MAX + 1)); // bool(false) — overflowed to float
?>This matters when validating user-supplied numbers that could be very large.
Checking numeric strings instead
If your value comes from a form, a URL, or a database, it is almost always a string — so is_int() will return false even for "42". For those cases you usually want one of these instead:
is_numeric()—truefor both numeric strings and numbers ("42",42,3.14).ctype_digit()—trueonly for strings made entirely of digit characters.- Filter validation —
filter_var($value, FILTER_VALIDATE_INT)returns the integer orfalse.
<?php
$fromForm = "42"; // string, as form input always is
var_dump(is_int($fromForm)); // bool(false)
var_dump(filter_var($fromForm, FILTER_VALIDATE_INT)); // int(42)
?>When to use it
Use is_int() when you need a strict type guarantee — for example, to confirm an internal value is genuinely an integer before using it as an array index, a loop bound, or an argument to code that assumes integers:
<?php
function repeat(string $text, int $times): string
{
// Defensive guard: reject anything that isn't a real int.
if (!is_int($times) || $times < 0) {
return $text;
}
return str_repeat($text, $times);
}
echo repeat("ab", 3) . "\n"; // ababab
?>For raw input validation, reach for is_numeric() or filter_var() instead, then cast to int.
Related functions
is_float()— test for the float type.is_string()— test for the string type.is_bool()— test for the boolean type.is_array()— test for the array type.is_numeric()— test for a number or numeric string.gettype()— get the type name as a string.intval()— convert a value to an integer.
Conclusion
is_int() checks whether a variable is stored as an integer, not whether it merely looks like one. Remember the gotchas: quoted numbers are strings, whole-valued floats like 42.0 are floats, and integers that overflow PHP_INT_MAX become floats. Use is_int() for strict internal type checks, and is_numeric() or filter_var() when validating strings from users.