happy cat image

everdevel

coding

login
NotificationX
  • Currently, only notices of comments are displayed.
  • no comment or please login

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";
    }
?>

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";
    }
?>

Result


Thank you for visiting. If you have any inquiry or explanation of mistakes, please use the comments below.


    
    

Back to the course

ALL COMMENTS 0

Sort by