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