Skip to main content

How to Check if a Value Is an Integer in PHP with is_int

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
$<result> = is_int(<value>);
PartWhat 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:

main.php, which declares overLimit with two int parameters, prints is_int for 500, 500.0, the string 500 and null with var_dump, then calls overLimit with a limit of the string 500 only when is_int says it's an integer.

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​

Terminal
php main.php
Output
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:

main.php
<?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));
Terminal
php main.php
Output
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

A zsh terminal where php main.php prints bool(true), then bool(false) three times, then the limit is not an integer; for the changed file it stops with PHP Fatal error: Uncaught TypeError: overLimit(): Argument #2 ($limit) must be of type int, string given, shown twice.

What Happened​

  • is_int(500) was true. 500.0, '500' and null were false: a float, a string and null are other types, even when they look like the number 500.
  • The string limit went to the else branch, so overLimit never saw it.
  • Without the check, overLimit(900, '500') stopped the script with TypeError: overLimit(): Argument #2 ($limit) must be of type int, string given, because strict_types makes PHP refuse a string for an int parameter. 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, as Fatal 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​

MistakeWhat happensDo this instead
Checking form input or other text with is_intText is always a string, so is_int is always falseCheck and convert it with filter_var($text, FILTER_VALIDATE_INT), which returns the integer or false
Using is_numeric to check for a whole numberis_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() insteadCall is_int()

Troubleshooting​

SymptomLikely causeFix
TypeError: overLimit(): Argument #2 ($limit) must be of type int, string givenA string reached an int parameter under strict_typesCheck it with is_int, or convert text with filter_var first
Deprecated: Function is_integer() is deprecated since 8.6, use is_int() insteadCode calls an old name of is_intCall 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​