What is the is_int method in PHP?

The is_int method can be used to check if a variable is an integer.

Syntax


is_int(mixed $value): bool

Return value

The is_int method returns true if the passed value is an integer. Otherwise, it returns false.

Code

<?php
$test = 10;
printIsInt($test);
$test = 10000;
printIsInt($test);
$test = 100.1;
printIsInt($test);
$test = '10';
printIsInt($test);
function printIsInt($test){
echo "The value is: ";
var_dump($test);
echo "Is Integer: ";
var_dump(is_int($test));
echo "---------\n\n";
}
?>

Explanation

In the code above:

  • We use the is_int method to check if the variable is an integer.

  • For the values 10 and 10000, the is_int method returns true.

  • For the values 100.1(float) and '10'(string), the is_int method returns false, as these are not integers.

Free Resources