PHP is_int() and is_numeric()

When coding with PHP, the first requisite is a proper handling of variables, as they are not typed. We can think that the built-in is_int() function will work correctly to detect integer values inside a variable...

Try this:

$param = '1';
echo is_int($param);

It will return false, because it checks that the type of the variable is an integer... not that the variable's value can be interpreted as an integer ;)

In order to properly test for numeric values, you have to always use is_numeric() (documentation), and then one of the following options:

a) Do a cast (this works ok to detect floats, but will still fail with integers):

$param = '12.5';
echo is_float((float)$param);

b) Create a regular expression to check numeric formats.

Once again, the problem of untyped languages rises up. With a correctly typed language this wouldn't happen.
And with an untyped one, I wonder why leaving so many misleading functions instead of deprecating them in favor of correctly working ones...

PHP is_int() and is_numeric() published @ . Author: