Using PHP is_int() Function to Check Integer Variables
This article explains the PHP is_int() function, its syntax, and provides clear code examples demonstrating how to determine whether variables are integers, helping developers correctly handle different data types in backend development.
PHP provides the built‑in is_int() function to determine whether a variable is of integer type, returning true for integers and false otherwise.
The function signature is bool is_int ( mixed $var ), where $var can be any type.
Example 1 defines three variables—a integer, a float, and a string—and uses var_dump(is_int(...)) to show the function returns true only for the integer.
<?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 creates three variables of different types and uses if (is_int(...)) { echo "..."; } else { echo "..."; } to output 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 checking variable types, helping developers handle data appropriately in backend applications.
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.
