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 Courses
php Courses
php Courses
Using PHP is_int() Function to Check Integer Variables

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 "<br/>";

if (is_int($var2)) {
    echo '$var2 是一个整型';
} else {
    echo '$var2 不是一个整型';
}

echo "<br/>";

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPtype checkingis_intinteger validation
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.