How To Check Valid Integer Values Using PHP Function Example

  • 20-07-2022
  • 760
  • PHP
  • Haresh Chauhan

How to check valid integer values using the PHP function?; In this post, I will learn you how to check whether the given veriable is an integer or not. Some time developer must need to check valid datatype for inserting into the database so this is more important in some cases.

To check given value is an integer or not we will take help of the PHP function filter_var(), This function will check and return the boolean value where the given value is a valid integer or not.

Accurate with integers this function will help us. In this function we must need to pass two arguments to check the valid integer, In The first argument we will pass the value, and in the second argument, we will pass the type of check with the valid integer. So let's take an example for deep understanding.

How To Remove HTML Tags From The PHP String

<?php
// EXAMPLE 1
$int = 166;

if (!filter_var($int, FILTER_VALIDATE_INT) === false) {

    echo("Integer is valid"); // TRUE
} else {
    echo("Integer is not valid");
}

// EXAMPLE 2

$string = '661';

if (!filter_var($string, FILTER_VALIDATE_INT) === false) {

    echo("Integer is valid");
} else {
    echo("Integer is not valid"); // FALSE
}


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :