Google News
logo
PHP Program to Check if the type of a variable is integer
In PHP, you can check the type of a variable using the `gettype()` function, which returns a string indicating the data type of the variable. To check if a variable is an integer, you can use the `is_int()` function, which returns `true` if the variable is an integer and `false` otherwise.

Here's an example :
Program :
<?php
    $num = 10;
    if (is_int($num)) {
        echo "The variable is an integer.";
    } else {
        echo "The variable is not an integer.";
    }
?>
Output :
The variable is an integer.
In this code, the variable `$num` is assigned the integer value `10`. The `if` statement checks if `$num` is an integer using the `is_int()` function.

If `$num` is an integer, then the code inside the `if` block is executed, which simply prints the message "The variable is an integer." Otherwise, the code inside the `else` block is executed, which prints the message "The variable is not an integer."

This demonstrates how you can use the `is_int()` function to check if a variable is an integer in PHP.