Types7 min readExamples run on PHP 8.5.10

is_integer()

Learn how PHP is_integer() checks whether a value has the integer type, why it is an alias of is_int(), and where numeric strings and floats surprise people.

Signature

is_integer(mixed $value): bool

Purpose

is_integer() answers one question: is the value you passed of PHP's int type? It is an alias of is_int(), so both names behave identically. Pick one and use it consistently in a codebase. The function inspects the type of the value, not its appearance, so it does not ask whether the value looks like a whole number.

Signature

Because the manual describes is_integer() as an alias of is_int(), the signature is the same as that of is_int().

Parameters and defaults

  • $value: any value at all. It is required and has no default. Integers, floats, strings, booleans, null, arrays, objects and resources can all be passed in.

Return value and failure cases

The function returns true when the value is of type int, and false for every other type. It does not throw for unexpected input, does not emit warnings, and does not convert the argument. There is no separate failure value, because every input produces a plain boolean.

Example 1: which types count?

Only a real integer passes. Floats, numeric strings, booleans and null do not, even when they could be converted to an integer.

Example
<?php
$values = [42, -7, 0, 3.5, "42", true, null, [1]];
foreach ($values as $value) {
    echo json_encode($value), ' => ', var_export(is_integer($value), true), "\n";
}
Output
42 => true
-7 => true
0 => true
3.5 => false
"42" => false
true => false
null => false
[1] => false

Notice that "42" is a string, so it is rejected. The quotes in the JSON output make that visible.

Example 2: checking the result of a parser

filter_var() with FILTER_VALIDATE_INT returns an integer on success and false on failure. Calling is_integer() on the result is a clear way to tell the two cases apart. A parsed 0 is a valid integer but is falsy, so a plain truthiness test would wrongly treat it as a failure.

Example
<?php
$inputs = ["15", "15.5", "abc", "-3"];
foreach ($inputs as $input) {
    $parsed = filter_var($input, FILTER_VALIDATE_INT);
    echo $input, ': ', is_integer($parsed) ? 'integer' : 'not integer', "\n";
}
Output
15: integer
15.5: not integer
abc: not integer
-3: integer

Example 3: overflow and arithmetic results

The type of an expression depends on the operation. Adding one to PHP_INT_MAX overflows into a float. Dividing with / yields an integer only when both operands are integers and the division is exact.

Example
<?php
$max = PHP_INT_MAX;
$next = $max + 1;
echo json_encode([is_integer($max), is_integer($next), is_integer(10 / 2), is_integer(10 / 4)]);
echo "\n";
Output
[true,false,true,false]

Edge cases

  • A float with no fractional part, such as 3.0, is still a float, so the result is false.
  • Numeric strings such as "7" or "0x1A" are strings and return false.
  • true, false and null are never integers.
  • Integer overflow produces a float, which is_integer() rejects.
  • -0 written as an integer literal is just 0, which is an integer.
  • In a file without strict_types, a value that was coerced when passed to a typed parameter may already be an int. Check the type at the point where you actually hold the value.

Common mistakes

  1. Expecting form input to pass. Request data and many text sources arrive as strings. Calling is_integer() on them returns false. Validate with filter_var() or convert deliberately first.
  2. Confusing it with is_numeric(). is_numeric("42") is true, while is_integer("42") is false. They answer different questions.
  3. Treating whole floats as integers. Results of functions such as floor() and round() are floats, so is_integer(floor(4.7)) is false.
  4. Mixing alias names. Using both is_int() and is_integer() in one project works, but it makes searching and reviewing harder.
  5. Testing truthiness instead of type. A parsed 0 is falsy. When a function can return false on failure, check the type rather than the value's truth.
  • is_int(): the primary name of the same check.
  • is_float(): tests for the float type.
  • is_numeric(): accepts integers, floats and numeric strings.
  • filter_var(): validates and converts text to an integer.
  • intval(): converts a value to an integer instead of checking it.
  • gettype() and get_debug_type(): report the type name.

Recap

is_integer() is a strict type check and an alias of is_int(). It takes one value of any type, never throws, and returns true only for values whose type is int. Numeric strings, whole-number floats and booleans all return false, so validate or convert input first when it may arrive in another type.

Tested examples

Which types count as integers
<?php
$values = [42, -7, 0, 3.5, "42", true, null, [1]];
foreach ($values as $value) {
    echo json_encode($value), ' => ', var_export(is_integer($value), true), "\n";
}
Output
42 => true
-7 => true
0 => true
3.5 => false
"42" => false
true => false
null => false
[1] => false
Checking the result of filter_var()
<?php
$inputs = ["15", "15.5", "abc", "-3"];
foreach ($inputs as $input) {
    $parsed = filter_var($input, FILTER_VALIDATE_INT);
    echo $input, ': ', is_integer($parsed) ? 'integer' : 'not integer', "\n";
}
Output
15: integer
15.5: not integer
abc: not integer
-3: integer
Overflow and division results
<?php
$max = PHP_INT_MAX;
$next = $max + 1;
echo json_encode([is_integer($max), is_integer($next), is_integer(10 / 2), is_integer(10 / 4)]);
echo "\n";
Output
[true,false,true,false]