is_int
Checks whether a variable is of type int, without converting numeric strings.
is_int(mixed $value): bool- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | mixed | The variable being evaluated. |
Return value
booltrue if $value is an int, false otherwise.
Changelog
- PHP 8.0.0The alias is_long() is still available; is_integer() also remains a valid alias for is_int().
is_int() is a strict type check. It is true only for values that are already integers — 23, -1, PHP_INT_MAX — and false for '23', 23.0, true and null. is_integer() and is_long() are aliases of it.
The string case is what bites. Almost everything that crosses a boundary hands you strings: $_GET and $_POST, CSV rows, environment configuration, and PDO drivers without native prepared statements or with emulation on. So is_int($_GET['page']) is always false, and a validator built on it silently rejects everything.
| Goal | Use |
|---|---|
| Value already has int type | is_int($v) |
| Int or numeric string | is_numeric($v) |
| Validate and convert input | filter_var($v, FILTER_VALIDATE_INT) |
| Digits only, no sign | ctype_digit($v) |
When to reach for it
It fits inside your application, after input has been parsed: asserting a repository returned an int ID, branching in a function with a int|string union parameter, or guarding a call to str_repeat(). At the edge, validate with filter_var() and convert, then the type check has something real to test.
Example
When to use it
- Branching inside a function whose parameter is declared int|string, such as a lookup by ID or by slug
- Asserting that a repository or cache returned a real int ID rather than a numeric string
- Detecting integer overflow by checking that an arithmetic result is still an int and not a float
Frequently asked questions
Why does is_int('42') return false?
Because '42' is a string, and is_int() checks the type rather than the content. Use is_numeric() or filter_var($v, FILTER_VALIDATE_INT) for numeric strings.
What is the difference between is_int(), is_integer() and is_long()?
They are the same function — is_integer() and is_long() are aliases of is_int(). Use is_int() in new code for consistency with the int type declaration.
How do I check that a value is a whole number even when it is a float?
Combine the checks: is_int($v) || (is_float($v) && floor($v) === $v). A float such as 23.0 is a whole number but is_int() correctly reports false for it.
More examples
Validating IDs that arrive as strings
Only the literal 7 passes is_int(). filter_var() accepts '7' and '-3', converting them to real ints, but rejects '07' (leading zeros are not valid without FILTER_FLAG_ALLOW_OCTAL), '7.0' and 'abc' — exactly the strictness you want at an input boundary.
<?php
$ids = [7, '7', '07', '7.0', '-3', 'abc'];
foreach ($ids as $id) {
$clean = is_int($id) ? $id : filter_var($id, FILTER_VALIDATE_INT);
printf("%-6s is_int=%-5s validated=%s\n",
var_export($id, true),
var_export(is_int($id), true),
var_export($clean, true)
);
}Overflow turns ints into floats
Adding one to PHP_INT_MAX silently produces a float, so is_int() is the cheapest overflow detector available. Division may or may not stay an int, while intdiv() and the modulo operator always return one.
<?php
var_dump(is_int(PHP_INT_MAX));
var_dump(is_int(PHP_INT_MAX + 1));
echo get_debug_type(PHP_INT_MAX + 1), "\n";
var_dump(is_int(intdiv(9, 2)));
var_dump(is_int(9 / 2));
var_dump(is_int(9 % 2));Branching on a union type
With a int|string union, is_int() is the correct discriminator inside the function. The last call shows the consequence: '42' is a string, so it takes the slug branch — the caller has to cast before calling if it meant an ID.
<?php
function find(int|string $key): string {
return is_int($key)
? "lookup by id: $key"
: "lookup by slug: $key";
}
echo find(42), "\n";
echo find('welcome-page'), "\n";
echo find('42'), "\n";
Discussion