QUIZGUM

Coding Class

Quizgum : 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()

  1. 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.

  1. <?php
  2. $int = 1000;
  3.  
  4. $checkInt = filter_Var($int, FILTER_VALIDATE_INT);
  5.  
  6. if($checkInt == true) {
  7. echo "This is Integer";
  8. } else {
  9. echo "This is not Integer";
  10. }
  11. ?>

Result

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

  1. <?php
  2. $int = 1000.1;
  3.  
  4. $checkInt = filter_Var($int, FILTER_VALIDATE_INT);
  5.  
  6. if($checkInt == true) {
  7. echo "This is Integer";
  8. } else {
  9. echo "This is not Integer";
  10. }
  11. ?>

Result