How to Check if a Value Is an Integer in PHP with is_int
is_int($value) in PHP returns true only when $value has PHP's integer type. A float such as 500.0 and a string of digits such as '500' aren't integers, so check a value that arrived as text before you pass it to a function that wants an int.
Syntax
$<result> = is_int(<value>);
| Part | What it does |
|---|---|
<value> | The value to check. It can be of any type. |
$<result> | true if <value> is an integer, otherwise false. |
The PHP manual: is_int "Finds whether the type of the given variable is integer." It adds a note: "To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric()."
The Example
The example's overLimit function takes two integers and says whether an amount is over a limit. The code checks four values with is_int: 500, 500.0, '500' and null. Then it checks a limit that arrived as the text '500' before passing it to overLimit, and finally passes it without the check.
Prerequisites
- PHP. This guide uses PHP 8.6.0.
Step 1: Write the Code
Create a file named main.php:

<?php
declare(strict_types=1);
function overLimit(int $amount, int $limit): bool
{
return $amount > $limit;
}
var_dump(is_int(500));
var_dump(is_int(500.0)); // a float, although nothing follows the point
var_dump(is_int('500')); // a string of digits
var_dump(is_int(null));
$limit = '500'; // a limit that arrived as text
if (is_int($limit)) {
var_dump(overLimit(900, $limit));
} else {
echo "the limit is not an integer\n";
}
declare(strict_types=1); makes PHP refuse a value of the wrong type for a typed parameter instead of converting it. var_dump prints a value with its type, so true shows as bool(true).
Step 2: Run It
php main.php
bool(true)
bool(false)
bool(false)
bool(false)
the limit is not an integer
The command is the same in zsh, bash and PowerShell.
Step 3: Remove the Check
Replace main.php with a version that passes the limit straight to overLimit:
<?php
declare(strict_types=1);
function overLimit(int $amount, int $limit): bool
{
return $amount > $limit;
}
$limit = '500'; // a limit that arrived as text
// Without the is_int check, a string reaches the int parameter.
var_dump(overLimit(900, $limit));
php main.php
PHP Fatal error: Uncaught TypeError: overLimit(): Argument #2 ($limit) must be of type int, string given, called in /Users/Shared/you/main.php on line 12 and defined in /Users/Shared/you/main.php:5
Stack trace:
#0 /Users/Shared/you/main.php(12): overLimit(900, '500')
#1 {main}
thrown in /Users/Shared/you/main.php on line 5
Fatal error: Uncaught TypeError: overLimit(): Argument #2 ($limit) must be of type int, string given, called in /Users/Shared/you/main.php on line 12 and defined in /Users/Shared/you/main.php:5
Stack trace:
#0 /Users/Shared/you/main.php(12): overLimit(900, '500')
#1 {main}
thrown in /Users/Shared/you/main.php on line 5

What Happened
is_int(500)wastrue.500.0,'500'andnullwerefalse: a float, a string andnullare other types, even when they look like the number 500.- The string limit went to the
elsebranch, sooverLimitnever saw it. - Without the check,
overLimit(900, '500')stopped the script withTypeError: overLimit(): Argument #2 ($limit) must be of type int, string given, becausestrict_typesmakes PHP refuse a string for anintparameter. The stack trace shows the call,overLimit(900, '500'), on line 12. - The error appears twice because this PHP's settings both log errors to the terminal, as
PHP Fatal error:, and display them, asFatal error:.
Without declare(strict_types=1), PHP converts a numeric string to an integer instead: in a separate check, the same call then returned true.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Checking form input or other text with is_int | Text is always a string, so is_int is always false | Check and convert it with filter_var($text, FILTER_VALIDATE_INT), which returns the integer or false |
Using is_numeric to check for a whole number | is_numeric is also true for strings such as '5e2' | Use filter_var with FILTER_VALIDATE_INT, which returned false for '5e2' and '5.0' |
Calling is_integer() or is_long() | PHP 8.6 prints Deprecated: Function is_integer() is deprecated since 8.6, use is_int() instead | Call is_int() |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
TypeError: overLimit(): Argument #2 ($limit) must be of type int, string given | A string reached an int parameter under strict_types | Check it with is_int, or convert text with filter_var first |
Deprecated: Function is_integer() is deprecated since 8.6, use is_int() instead | Code calls an old name of is_int | Call is_int() |
FAQ
What does filter_var return for a string of digits?
The integer: filter_var('500', FILTER_VALIDATE_INT) returned int(500). The PHP manual says FILTER_VALIDATE_INT "Validates whether the value is an integer, on success it is converted to type int."
Is a number bigger than PHP_INT_MAX an integer?
No. PHP_INT_MAX was 9223372036854775807 here, and PHP_INT_MAX + 1 became the float 9.223372036854776E+18, for which is_int returned false.
Do numbers from json_decode come back as integers?
Whole numbers do. In my check, 500 in the JSON came back as an integer, while 500.0 came back as a float and "500" as a string.
Why are is_integer and is_long deprecated?
They're old names for is_int. PHP 8.6 deprecates both, following the Deprecations for PHP 8.6 RFC, which calls each of them "a legacy alias of is_int()".
Conclusion
is_int is true only for values of PHP's integer type, never for floats or strings that look like integers. Check a value before passing it to an int parameter, and use filter_var with FILTER_VALIDATE_INT to turn text into an integer.
Additional Resources
- is_int and is_numeric, in the PHP manual.
- Validate filters, in the PHP manual, with
FILTER_VALIDATE_INT. - Integers, in the PHP manual.
