Using PHP is_int() Function to Check Integer Variables
This article explains the PHP is_int() function, its syntax, return values, and provides two practical code examples demonstrating how to determine whether variables are integers, helping developers handle different data types correctly.
PHP is a widely used scripting language for web development that provides many built‑in functions, including is_int(), which checks whether a variable is an integer and returns a boolean.
The syntax of is_int() is: bool is_int ( mixed $var ) The function accepts a single argument of any type and returns true if the argument is an integer, otherwise false.
Example 1 demonstrates three variables ($num1, $num2, $str) and uses var_dump(is_int(...)) to show the function returns true for the integer and false for a float and a string.
<?php
$num1 = 10; // integer
$num2 = 10.5; // float
$str = "10"; // string
var_dump(is_int($num1)); // bool(true)
var_dump(is_int($num2)); // bool(false)
var_dump(is_int($str)); // bool(false)
?>Example 2 defines three variables ($var1, $var2, $var3) and uses if (is_int(...)) statements to echo messages indicating whether each variable is an integer.
<?php
$var1 = 123;
$var2 = "abc";
$var3 = true;
if (is_int($var1)) {
echo "Variable 1 is an integer";
} else {
echo "Variable 1 is not an integer";
}
if (is_int($var2)) {
echo "Variable 2 is an integer";
} else {
echo "Variable 2 is not an integer";
}
if (is_int($var3)) {
echo "Variable 3 is an integer";
} else {
echo "Variable 3 is not an integer";
}
?>In summary, is_int() is a useful PHP function for quickly determining if a variable holds an integer, which helps developers handle different data types appropriately.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
