Oct 17, 2012

PHP: is_int() vs is_numeric()

Both of the functions looks similar, but there is a difference, which can screw your time if you’re not aware of it and using blindly! is_int() seems same to is_numeric(), checking the variable if it’s integer or not, but it’s not exactly what you’re thinking.

The key difference between these two functions is that is_int() checks the type of variable, while is_numeric() checks the value of the variable.

From PHP.net,
is_int: Find whether the type of a variable is integer
is_numeric: Finds whether a variable is a number or a numeric string

So, if you check something like:

$var = "123";

$var is a string of numbers, not an integer value.
Therefor is_int should return false as it’s not an integer, it’s a string.
However it is a numeric string, so hence is_numeric should return true.

Leave a comment


Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!




Magento top 50 contributors

Magento top 50 contributors