CLASS web

PHP

integer validation

Validate Integers

Let's learn how to validate integers.
Use the filter_Var() built-in function.
And use the constant FILTER_VALIDATE_INT as argument.

How to validate integers using filter_Var()

filter_Var(integer,FILTER_VALIDATE_INT);

The first argument is an integer and the second uses the constant FILTER_VALIDATE_INT.
As shown in the example, you can change the purpose according to the value of a constant.
Returns true if the integer is valid and false.
The following example validates an integer.

<?php
    $int = 1000;

    $checkInt = filter_Var($int, FILTER_VALIDATE_INT);

    if($checkInt == true) {
        echo "This is Integer";
    } else {
        echo "This is not Integer";
    }
?>

I'll check the results right below.







Result

This time, let's test it by entering the wrong integer.

<?php
    $int = 1000.1;

    $checkInt = filter_Var($int, FILTER_VALIDATE_INT);

    if($checkInt == true) {
        echo "This is Integer";
    } else {
        echo "This is not Integer";
    }
?>

I'll check the results right below.







Result





ALL COMMENTS 0

Sort by

PinkCoding

PinkCoding

X

PinkCoding

Web Scratch Pad

X

loading

            
            
        

Back to the course