What is the safest way to check if a variable is an integer in PHP when dealing with form inputs?

When dealing with form inputs in PHP, it is important to validate and sanitize user input to prevent security vulnerabilities and errors in the application. To check if a variable is an integer, the safest way is to use the "filter_var" function with the FILTER_VALIDATE_INT filter. This function will return the integer value if the variable is an integer, or false if it is not.

$input = $_POST['input']; // assuming 'input' is the name of the form field

if (filter_var($input, FILTER_VALIDATE_INT) !== false) {
    // Variable is an integer
    $integerValue = (int) $input;
    echo "The input is an integer: $integerValue";
} else {
    // Variable is not an integer
    echo "The input is not an integer";
}