Google News
logo
PHP Program to Validate an integer
In the following example of PHP program to validate an integer using the `filter_var()` function :
Program :
<?php
$int = "1234";

if (filter_var($int, FILTER_VALIDATE_INT)) {
    echo("$int is a valid integer");
} else {
    echo("$int is not a valid integer");
}
?>
Output :
1234 is a valid integer
In this program, we define a variable `$int` and assign it the value `"1234"`. We then use the `filter_var()` function to validate this variable as an integer using the `FILTER_VALIDATE_INT` filter.

If the variable is a valid integer, the program will output "`1234 is a valid integer`". Otherwise, the program will output "`1234 is not a valid integer`".