What is PHP is_integer() Function?
If you want to check whether a variable’s type is integer or not, use is_integer() function.
Few important things about integer-
- An integer is a whole number, no decimal part included in it. Ex. 1,100000 etc.
- PHP supports signed integer i.e. it can be both positive or negative.
- You can define an integer in four formats –
- Decimal (10 based): Example- 100
- Hexadecimal (16 based, starts with 0x or 0X): Example- 0xA
- Octal (8 based, starts with 0o or 0O or 0): Example- 0o12
- Binary (2 based, starts with 0b or 0B): Example 0b1010
- Integer size is platform-dependent. Using the following constants, you can find out about your system’s integer support-
- PHP_INT_MAX: Largest possible integer value. In my system, it is 9223372036854775807.
- PHP_INT_MIN: Smallest possible integer value. In my system it is -9223372036854775808.
- PHP_INT_SIZE: Integer size in bytes. In my system, it is 8.
Syntax:
is_integer(variable)
Parameters:
The Function has parameter which is required-
variable (Required): It specifies a value.
Return Values:
The function returns-
- TRUE –if the value is of integer type.
- FALSE – if the value is not of integer type.
Examples:
Example 1:
<?php
$var1 = "PHP";
$var2 = 100;
$var3 = "100";
$var4 = 1.5;
$var5 = FALSE;
$var6 = [1,2];
$var7 = NULL;
echo "Is $var1 a integer value? " . (is_integer($var1) ? 'Yes' : 'No') . "<br />";
echo "Is $var2 a integer value? " . (is_integer($var2) ? 'Yes' : 'No') . "<br />";
echo "Is \"$var3\" a integer value? " . (is_integer($var3) ? 'Yes' : 'No') . "<br />";
echo "Is $var4 a integer value? " . (is_integer($var4) ? 'Yes' : 'No') . "<br />";
echo "Is $var5 a integer value? " . (is_integer($var5) ? 'Yes' : 'No') . "<br />";
echo "Is [1,2] a integer value? " . (is_integer($var6) ? 'Yes' : 'No') . "<br />";
echo "Is NULL a integer value? " . (is_integer($var7) ? 'Yes' : 'No');
?>
Output:
Is PHP an integer value? No
Is 100 an integer value? Yes
Is "100" an integer value? No
Is 1.5 an integer value? No
Is FALSE an integer value? No
Is [1,2] an integer value? No
Is NULL an integer value? No
Practical Usages of is_integer() Function:
- You can validate of integer type of form inputs with this function
- You can validate a integer value before inserting into database field which only accept integer value.
- When separating integers from other types of values from an array or files, you can use this function.
Notes on is_integer() Function:
- is_integer() is alias for the function is_int() function.
- Though, modern PHP supports integer() function, it is recommended to use the shorter one – int() function.
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP is_integer() Function
is_integer() is a built-in variable handling function. Use this function when you need strict type checking for a number value.