PHP is_numeric() vs is_int()

Some lessons you don’t forget, but this one I did because it doesn’t come up very often.

Don’t use is_int() because, as Jeremy says, it’ll lie to you. Use is_numeric() instead.

Copy the following chunk of code into a php file and run it. You’ll be surprised at the outcome:

$t = "12345";
if( is_int($t ) ) {
    echo $t . " is an int!";
} else {
    echo $t . " is not an int!";
}

The problem is that is_int() thinks a string of numbers is a string, not an integer. The is_int() man page has an example illustrating that but it’s easy to miss. That function should carry a public health warning!

There’s also the ctype_digit() function too but it has it’s own gotcha:

Note: This function require a string to be useful, so for example passing in an integer will always return FALSE. However, also note that HTML Forms will result in numeric strings and not integers. See also the types section of the manual.

13 thoughts on “PHP is_numeric() vs is_int()

  1. The naming of those two functions is probably too subtle. The key difference between the two is the one checks the type of variable, is_int(), and the other checks the value of the variable, is_numeric().

  2. Actually this sort of confusion is more widespread than you might think.

    Certainly in Oracle 7 (but it might have changed in later versions) if you had a VARCHAR2 column then it was perfectly legitimate to do:

    If column=27 then do something.

    as long as the column only ever contained numbers. The minute any row in that column contained a character then the If logic given above would break with a type mismatch because the column and the value were no longer the same type. I know, I know.. they weren’t to start with but it was obviously something to do with how Oracle typed its data structures.

  3. I think the real less here is don’t use is_int if you’re not looking to find out if the variable in question is of type int :)

    Maybe my Java-based introduction to programming is showing, but that differentiation wasn’t revelation to me.

  4. “The problem is that is_int() thinks a string of numbers is a string, not an integer.”

    It’s not a problem, it tells you which data type the variable really holds.

    • (int) is for casting to an int. is_int() is for type checking.

      $foo = (int) 'bar'; // 0
      $foo = (int) 8.2; // 8
      $foo = (int) '8'; // 8

      is_int(8); // true
      is_int('foobar'); // false

      Completely different.

    • Dankoozy, it really depends on your coding style and sometimes the specifics of your application. This can be a good way to catch errors in your code. Let’s say I have a load_user function that takes a user ID and loads it from the database. I might want to throw an exception when the input is not an int. However, in most applications which use “1″ and 1 interchangeably, this would create a lot of false positives.

      One could go through entire projects without ever wanting to use is_int, but sometimes you want to error out when a non-int is provided, as opposed to just casting it to an int.

  5. It’s important to note the description of the function before using it.

    (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

    PHP is a loosely typed language. It doesn’t enforce typing, but it does keep track of types to a degree. Since it’s loosely typed, you can usually avoid functions that talk about the *type* of a variable. For example, is_int helps you probe the *type* of a variable, which is very different from the *contents* of the variable (even in a loosely typed language like PHP).

    is_numeric is concerned with the *contents* of the variable, not the type. It tells you if the variable is a number (123) or a numeric string (“123″).

  6. It’s a type check. It’s doing what it is suppose to. If you’re looking to ensure a value, than use a filter_var instead.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>