PHP is_integer() Function
The PHP is_integer() function checks whether a variable is of type integer. The function returns true if the variable is an integer, otherwise it returns false. This function is an alias of is_int() function.
Note: To check whether a variable is a number or a numeric string (such as form input, which is always a string), use is_numeric() function.
Syntax
is_integer(variable)
Parameters
variable |
Required. Specify the variable being evaluated. |
Return Value
Returns true if variable is an integer, false otherwise.
Example:
The example below shows the usage of is_integer() function.
<?php var_dump(is_integer(10)); //returns: bool(true) echo "\n"; var_dump(is_integer(10.5)); //returns: bool(false) var_dump(is_integer(1e5)); //returns: bool(false) var_dump(is_integer('10.5')); //returns: bool(false) var_dump(is_integer('10')); //returns: bool(false) var_dump(is_integer('xyz')); //returns: bool(false) var_dump(is_integer('1e5')); //returns: bool(false) var_dump(is_integer(true)); //returns: bool(false) ?>
The output of the above code will be:
bool(true) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false)
Example:
Consider one more example where this function is used to check all elements of an array whether they are of type integer or not.
<?php $Arr = array(10, "10", 10.5, "10.5", null, false, "true", 1e3, "1e3"); foreach ($Arr as $value) { echo "is_integer("; var_export($value); echo ") = "; var_dump(is_integer($value)); } ?>
The output of the above code will be:
is_integer(10) = bool(true) is_integer('10') = bool(false) is_integer(10.5) = bool(false) is_integer('10.5') = bool(false) is_integer(NULL) = bool(false) is_integer(false) = bool(false) is_integer('true') = bool(false) is_integer(1000.0) = bool(false) is_integer('1e3') = bool(false)
❮ PHP Variable Handling Reference