Master PHP’s is_int(): Quick Guide with Real Code Examples

This article explains PHP's is_int() function, its syntax, return values, and provides two practical code examples demonstrating how to check whether variables are integers, helping developers handle type checking efficiently.

php Courses
php Courses
php Courses
Master PHP’s is_int(): Quick Guide with Real Code Examples

PHP is a widely used scripting language for web development, offering many built‑in functions. One useful function is is_int(), which checks whether a variable is an integer, helping developers quickly determine a variable’s type.

The syntax of is_int() is: bool is_int ( mixed $var ) The function accepts a single parameter $var of any type and returns true if the variable is an integer, otherwise false.

Example 1

<?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)
?>

In this example three variables are defined: $num1, $num2, and $str. Using var_dump() to display the result of is_int() shows true for the integer and false for the float and string.

Example 2

<?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";
}
?>

This example defines three variables and uses if statements with is_int() to output messages indicating whether each variable is an integer.

In summary, is_int() is a handy PHP function for quickly checking if a variable is an integer, which is essential when handling different data types in development.

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.

PHPCode Examplestype checkingis_int
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.