Using PHP is_int() Function to Check Integer Variables

This article explains PHP's is_int() function, its syntax, return values, and provides two practical code examples demonstrating how to determine whether variables are integers, illustrating usage with integers, floats, strings, and boolean values.

php Courses
php Courses
php Courses
Using PHP is_int() Function to Check Integer Variables

PHP is a widely used scripting language for web development that provides many built‑in functions for handling various data types. One particularly useful function is is_int(), which checks whether a variable is an integer, helping developers quickly determine a variable's type for appropriate processing.

The syntax of is_int() is:

<code style='font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace; font-size: 12px; display: -webkit-box; padding: 15px 16px 16px; color: rgb(171, 178, 191)'>bool is_int ( mixed <span style="color: rgb(209, 154, 102); line-height: 26px">$var</span> )<br/></code>

This function accepts a single argument $var, which can be of any type (integer, float, string, etc.). It returns a boolean: true if the argument is an integer, otherwise false.

Below are two concrete code examples to illustrate the usage of is_int():

Example 1:

<code style='font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace; font-size: 12px; display: -webkit-box; padding: 15px 16px 16px; color: rgb(171, 178, 191)'><?php<br/>$num1 = 10; // integer<br/>$num2 = 10.5; // float<br/>$str = "10"; // string<br/><br/>var_dump(is_int($num1)); // outputs: bool(true)<br/>var_dump(is_int($num2)); // outputs: bool(false)<br/>var_dump(is_int($str)); // outputs: bool(false)<br/>?><br/></code>

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

Example 2:

<code style='font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace; font-size: 12px; display: -webkit-box; padding: 15px 16px 16px; color: rgb(171, 178, 191)'><?php<br/>$var1 = 123;<br/>$var2 = "abc";<br/>$var3 = true;<br/><br/>if (is_int($var1)) {<br/>    echo "Variable 1 is an integer";<br/>} else {<br/>    echo "Variable 1 is not an integer";<br/>}<br/><br/>if (is_int($var2)) {<br/>    echo "Variable 2 is an integer";<br/>} else {<br/>    echo "Variable 2 is not an integer";<br/>}<br/><br/>if (is_int($var3)) {<br/>    echo "Variable 3 is an integer";<br/>} else {<br/>    echo "Variable 3 is not an integer";<br/>}<br/>?><br/></code>

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

In summary, the is_int() function is a valuable PHP utility for quickly checking if a variable is an integer, enabling developers to handle different data types appropriately in their applications.

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.

PHPfunctionstype 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.