What is difference between is_int() vs is_integer() vs is_numeric() in php?

In this article, We’ll see what is the difference between is_int(), is_integer() and is_numeric() in php?

Above functions looks like similar, but there is a difference.

is_int(): It is used to check the type of a variable is integer.

is_integer(): alias of is_int(). It is also used to test whether the type of the specified variable is an integer or not.

is_numeric(): It is used to check the variable is a number or a numeric string.

Let’s see the example:

 $variable = "12345";
 echo is_int($variable);      // return false
 echo is_integer($variable);  // return false
 echo is_numeric($variable);  // return true 

$variable is a string of numbers, not an integer value.

is_int() return false because it’s not an integer value, it’s a string.

is_numeric() return true because it’s a numeric string.

 $variable2 = 123;
 echo is_int($variable2);      // return true
 echo is_integer($variable2);  // return true
 echo is_numeric($variable2);  // return true

$variable2 is a integer value, not a string of numbers.

is_int($variable2) return true because it’s an integer value, not a string.

is_numeric($variable2) return true because it’s a number.

That’s it!. Please share your thoughts or suggestions in the comments below.

Posted in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *