W3docs

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
ParameterDescription
$valueThe 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, use intval() or an (int) cast.

Basic example

php— editable, runs on the server

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()true for both numeric strings and numbers ("42", 42, 3.14).
  • ctype_digit()true only for strings made entirely of digit characters.
  • Filter validation — filter_var($value, FILTER_VALIDATE_INT) returns the integer or false.
<?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.

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.

Practice

Practice
Which of the following statement(s) is/are true about the is_int() function in PHP?
Which of the following statement(s) is/are true about the is_int() function in PHP?
Was this page helpful?