Definition and Usage
- PHP Version
- 4+
The is_int() function checks if a given value is of the integer type.
It returns true if the value is an integer, and false otherwise.
The integer type represents a data type used to express numeric values without decimal points.
In PHP, although strings and floating-point numbers can be treated as numbers depending on the context, the integer type itself only contains whole numbers without decimal points.
Basic Example
/* Integer type */
var_dump(is_int(1)); // bool(true)
var_dump(is_int(0)); // bool(true)
var_dump(is_int(-10)); // bool(true)
/* Floating-point numbers (float) */
var_dump(is_int(1.0)); // bool(false)
var_dump(is_int(0.0)); // bool(false)
var_dump(is_int(3.14)); // bool(false)
/* Strings */
var_dump(is_int('1')); // bool(false)
var_dump(is_int('0')); // bool(false)
var_dump(is_int('10')); // bool(false)
/* Results of numeric operations */
var_dump(is_int(1 + 1)); // bool(true)
var_dump(is_int(10 / 2)); // bool(true)
var_dump(is_int(9 / 2)); // bool(false)
/* Type casting */
var_dump(is_int((int) '1')); // bool(true)
var_dump(is_int((int) 1.9)); // bool(true)
Note:
The gettype() function returns the data type of the given value as a string.
Syntax
is_int(mixed $value): bool
Parameters
$value |
Required. The value to be checked for the integer type. |
|---|
Return Values
If the value passed as a parameter is of the integer type, the function returns true; otherwise, it returns false.
Things to Keep in Mind
There are several important points to be aware of when using the is_int() function.
Numeric Strings Such as '1' Return false
$num = '10';
var_dump(is_int($num)); // bool(false)
Floating-Point Numbers Such as 10.5 Return false
$num_1 = 10.5;
$num_2 = 10.0;
var_dump(is_int($num_1)); // bool(false)
var_dump(is_int($num_2)); // bool(false)
Integers That Start with 0, Such as 0123, Return true
$num_1 = 0123;
$num_2 = -0123;
$num_3 = 000;
var_dump(is_int($num_1)); // bool(true)
var_dump(is_int($num_2)); // bool(true)
var_dump(is_int($num_3)); // bool(true)
Practical Examples
The is_int() function is useful in situations where you need to check whether a value is of the integer type.
Checking Whether a Variable Is an Integer
When you need to determine whether a variable is an integer, using the is_int() function helps keep your code concise and clear. For example, the following code checks whether a variable is of the integer type and performs different actions accordingly.
$a = '2';
if (is_int($a)) {
echo 'Variable a is of the integer type.';
} else {
echo 'Variable a is not of the integer type.';
}
// Output: 'Variable a is not of the integer type.'
As shown in the next example, when you need to verify whether a value retrieved from a database is an integer, you can write your code as follows.
$value = $db->query('SELECT value FROM table WHERE id = 1')->fetchColumn();
if (!is_int($value)) {
echo 'The data type is not valid.';
exit;
}
// ... remaining processing
Input Validation
You can use this function to check whether a value entered by a user is an integer. For example, when receiving an age value from a form, you can write code like the following.
$user_input = $_POST['user_input'];
if (is_int($user_input)) {
// Handle valid integer input
} else {
// Handle invalid input
}
Performing Calculations
Before performing calculations that only allow integer values, you can use is_int() to validate the input. For example, when adding two numbers, you can write code as follows.
$num1 = 10;
$num2 = 20;
if (!is_int($num1) || !is_int($num2)) {
echo 'Addition can only be performed on integers.';
exit;
}
$sum = $num1 + $num2;
echo "The sum of the two numbers is {$sum}.";
// Output: 'The sum of the two numbers is 30.'
References
See also
- PHP gettype() Function – Get the Data Type of a Variable as a String
- PHP is_bool() Function – Checking the Boolean Type of a Variable
- PHP is_float() Function – Checking the Float Type of a Variable
- PHP is_numeric() Function – Checking Whether a Variable’s Value Is a Number or a Numeric String
- PHP is_string() Function – Checking the String Type of a Variable
- PHP isset() and empty() Functions – Concepts, Usage, and Key Differences
- PHP Superglobals – Simple and Essential Usage Guide