Signature
is_int(mixed $value): boolPurpose
is_int() answers one narrow question: is the value you passed stored as a PHP int? It looks at the type only, never at what the value could be converted to. That makes it a good guard for code that must receive a real integer, and a poor choice for checking text a user typed.
Signature
Parameters and defaults
$value: any value at all. There are no other parameters and no defaults. Because the type ismixed, nothing is coerced before the check, sodeclare(strict_types=1)makes no difference to the result.
Return value and failure cases
The function returns true when the argument is of type int and false for every other type. It does not throw, warn or return anything except a boolean, whatever you pass in: strings, floats, null, booleans, arrays, objects and so on all simply give false.
Example 1: which values are integers?
The first example runs a mixed list through the function. Only genuine integers pass, including zero, negative numbers and the largest integer the platform supports. The boolean true does not count, even though it converts to 1 in arithmetic.
$values = [23, "23", 23.5, null, true, 0, -7, PHP_INT_MAX];
$result = [];
foreach ($values as $value) {
$result[] = is_int($value);
}
echo json_encode($result), "\n";[true,false,false,false,false,true,true,true]
Example 2: form-style input
Data from forms, query strings and similar sources arrives as strings. is_int() therefore returns false for all of it, even for "42". If you want to know whether a string looks like a number, use is_numeric(). If you want to know whether it is a whole number and also receive it as an int, filter_var() with FILTER_VALIDATE_INT is usually the better tool.
$inputs = ["42", "-15", "4.2", "abc", "1e3"];
$out = [];
foreach ($inputs as $input) {
$out[$input] = [
"is_int" => is_int($input),
"is_numeric" => is_numeric($input),
"filter" => filter_var($input, FILTER_VALIDATE_INT),
];
}
echo json_encode($out, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";{"42":{"is_int":false,"is_numeric":true,"filter":42},"-15":{"is_int":false,"is_numeric":true,"filter":-15},"4.2":{"is_int":false,"is_numeric":true,"filter":false},"abc":{"is_int":false,"is_numeric":false,"filter":false},"1e3":{"is_int":false,"is_numeric":true,"filter":false}}
Notice that "1e3" is numeric but is not accepted as an integer by the filter, and filter_var() returns false on failure but the converted int on success.
Example 3: results of calculations
The type of an expression depends on the operation, so the answer is not always obvious. Dividing two integers yields an int only when the result is exact. Overflowing past PHP_INT_MAX produces a float. round() always returns a float, and an explicit (int) cast always produces an integer.
$tenDivTwo = 10 / 2;
$tenDivFour = 10 / 4;
$whole = intdiv(10, 2);
$overflow = PHP_INT_MAX + 1;
$rounded = round(2.4);
$cast = (int) "12abc";
echo json_encode([
"ten_div_two" => is_int($tenDivTwo),
"ten_div_four" => is_int($tenDivFour),
"intdiv" => is_int($whole),
"overflow" => is_int($overflow),
"round" => is_int($rounded),
"cast" => is_int($cast),
]), "\n";{"ten_div_two":true,"ten_div_four":false,"intdiv":true,"overflow":false,"round":false,"cast":true}
Edge cases
- A float with no fractional part, such as
5.0, is still a float, sois_int(5.0)isfalse. - Numeric strings, including
"0"and"-3", are strings and returnfalse. true,falseandnullare never integers.- Arrays, objects and resources return
false; the function does not look inside them. - Integer overflow silently turns a result into a float, so a value you expected to be an integer can fail the check after arithmetic near
PHP_INT_MAX.
Common mistakes
- Validating user input with
is_int(). Request data is text, so the check fails every time. Convert or validate it first, for example withfilter_var(). - Treating
is_numeric()as an integer test. It accepts floats, exponent notation and numeric strings. It answers a different question. - Assuming a rounded value is an integer.
round(),floor()andceil()return floats. Cast the result with(int)if you need anint, and check the range first. - Casting before checking.
(int) "12abc"gives12, so a cast followed byis_int()always passes and hides bad input. - Forgetting that booleans are separate. Do not rely on
truebehaving like1when a function must accept integers only.
Related functions
is_float()checks for the float type.is_numeric()accepts numbers and numeric strings.is_string()checks for strings.is_bool()checks for booleans.is_array()andis_object()check for compound types.filter_var()withFILTER_VALIDATE_INTvalidates and converts text.
Recap
is_int() takes any value and returns a plain boolean that is true only for values whose type is int. It never converts, never throws and ignores strict-types mode. Use it for type checks on values you already hold, and reach for is_numeric() or filter_var() when the data came from text.
Tested examples
$values = [23, "23", 23.5, null, true, 0, -7, PHP_INT_MAX];
$result = [];
foreach ($values as $value) {
$result[] = is_int($value);
}
echo json_encode($result), "\n";
[true,false,false,false,false,true,true,true]
$inputs = ["42", "-15", "4.2", "abc", "1e3"];
$out = [];
foreach ($inputs as $input) {
$out[$input] = [
"is_int" => is_int($input),
"is_numeric" => is_numeric($input),
"filter" => filter_var($input, FILTER_VALIDATE_INT),
];
}
echo json_encode($out, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
{"42":{"is_int":false,"is_numeric":true,"filter":42},"-15":{"is_int":false,"is_numeric":true,"filter":-15},"4.2":{"is_int":false,"is_numeric":true,"filter":false},"abc":{"is_int":false,"is_numeric":false,"filter":false},"1e3":{"is_int":false,"is_numeric":true,"filter":false}}
$tenDivTwo = 10 / 2;
$tenDivFour = 10 / 4;
$whole = intdiv(10, 2);
$overflow = PHP_INT_MAX + 1;
$rounded = round(2.4);
$cast = (int) "12abc";
echo json_encode([
"ten_div_two" => is_int($tenDivTwo),
"ten_div_four" => is_int($tenDivFour),
"intdiv" => is_int($whole),
"overflow" => is_int($overflow),
"round" => is_int($rounded),
"cast" => is_int($cast),
]), "\n";
{"ten_div_two":true,"ten_div_four":false,"intdiv":true,"overflow":false,"round":false,"cast":true}