W3docs

is_integer()

The is_integer() function is a deprecated alias of the is_int() function in PHP. It checks whether a variable is an integer or not. An integer is a data type

Introduction

is_integer() is an alias of is_int() in PHP. It tests whether a variable's type is integer and returns a boolean. An integer in PHP is a whole number without a fractional part, such as -5, 0, or 42.

This page covers what is_integer() checks (and what it deliberately does not), the gotchas around numeric strings, and which name you should actually use in new code.

Use is_int() instead. is_integer() and is_int() are the same function, so neither is removed or deprecated. However, is_int() is the canonical name used throughout the PHP manual and modern codebases. Prefer it for consistency.

Syntax

is_integer(mixed $value): bool

It takes one argument, $value, the variable to inspect, and returns true only when $value is of type int, and false for every other type.

Example Usage

php— editable, runs on the server

$var1 holds the integer 42, so the call returns true, which echo prints as 1. $var2 is a string, so the call returns false, which echo prints as nothing (an empty string). To see the boolean values clearly, use var_dump() instead of echo.

It checks the type, not the value

This is the most common source of confusion: is_integer() looks at the variable's type, so a string like "42" or a float like 42.0 is not an integer, even though it "looks like" one.

<?php
var_dump(is_integer(42));        // bool(true)
var_dump(is_integer("42"));      // bool(false) — numeric string, not int
var_dump(is_integer(42.0));      // bool(false) — float, not int
var_dump(is_integer(true));      // bool(false) — bool, not int
var_dump(is_integer(null));      // bool(false)
var_dump(is_integer(PHP_INT_MAX)); // bool(true)
?>

If you need to accept numeric strings (for example, values coming from $_GET, $_POST, or a CSV file), use is_numeric() or cast/validate with intval() instead.

When would I use this?

  • Guarding a function argument before doing integer-only math, e.g. using a value as an array index or a loop bound.
  • Differentiating types after a function that can return mixed types (a common pattern with strpos(), which returns either an int offset or false):
<?php
$pos = strpos("hello world", "world");
if (is_integer($pos)) {
    echo "Found at index $pos\n"; // Found at index 6
} else {
    echo "Not found\n";
}
?>
FunctionReturns true for
is_int()An int (canonical name; identical to is_integer())
is_float()A float (e.g. 3.14, 42.0)
is_string()A string, including "42"
is_numeric()An int, float, or numeric string
gettype()(returns the type name, not a bool)

See the PHP data types chapter for the full picture of how PHP categorizes values.

Conclusion

is_integer() is an alias of is_int() that returns true only when a variable is of type int. Remember that it checks the type, not the value, so numeric strings and floats return false. For new code, prefer the canonical is_int(); reach for is_numeric() when numeric strings should also count.

Practice

Practice
How can you check if a variable contains an integer value in PHP?
How can you check if a variable contains an integer value in PHP?
Was this page helpful?