Using PHP is_int() Function to Check Integer Variables
This article explains PHP's is_int() function, its syntax, and how it returns a boolean indicating whether a given variable is of integer type, accompanied by a complete code example demonstrating checks on integer, string, and float variables, and discusses its limitations and the alternative intval function.
PHP is a widely used scripting language in web development, providing many useful functions for handling data. One of them is the is_int function, which can be used to check whether a variable is an integer.
The is_int function syntax is as follows:
bool is_int (mixed $var)In this signature, $var is the variable to be checked. The function returns a boolean value: true if the variable is of integer type, otherwise false .
Below is an example code using the is_int function:
<?php
$var1 = 10;
$var2 = "5";
$var3 = 3.14;
if (is_int($var1)) {
echo '$var1 是一个整型';
} else {
echo '$var1 不是一个整型';
}
echo "
";
if (is_int($var2)) {
echo '$var2 是一个整型';
} else {
echo '$var2 不是一个整型';
}
echo "
";
if (is_int($var3)) {
echo '$var3 是一个整型';
} else {
echo '$var3 不是一个整型';
}
?>The above code produces the following output:
$var1 是一个整型
$var2 不是一个整型
$var3 不是一个整型From the output we can see that the is_int function correctly determines whether a variable is an integer. In this example, $var1 is an integer, $var2 is a string, and $var3 is a floating‑point number.
Note that is_int only checks the variable's type, not whether its value represents an integer. To validate the value, you can use other functions such as intval .
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.