PHP is_int() Function
Quick summary: The PHP is_int() function checks whether a variable is of type integer.
PHP is_int() Syntax
is_int(mixed $value): bool
PHP
PHP is_int() Basic examples
var_dump(is_int(10));
PHP
Output:
bool(true)
Returns true for integer values.
var_dump(is_int("10"));
PHP
Output:
bool(false)
Numeric strings are not integers.
PHP is_int() Real-world usage
$id = 5;
if (is_int($id)) {
echo 'Valid ID';
}
PHP
Output:
Valid ID
Ensures an ID is an integer.
PHP is_int() Edge cases
var_dump(is_int(10.0));
PHP
Output:
bool(false)
Floats are not integers even if they look like one.
PHP is_int() Common mistakes
Confusing numeric strings with integers
is_int checks type, not value.
Incorrect
is_int("123");
Correct
ctype_digit("123");
Use ctype_digit for numeric strings.
PHP is_int() Frequently Asked Questions
What does is_int() do?
Checks if a variable is integer.
Alias?
is_integer().
Use case?
Numeric validation.
Handles numeric strings?
No.
Common mistake?
Using with strings.
Return type?
bool.
Strict?
Yes.
Alternative?
ctype_digit().
Performance?
Fast.
Safe?
Yes.
Handles floats?
No.
Best practice?
Validate before casting.