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.

19 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.

    1. Oracle always stores it value according to the declared type, in this example COLUMN is stored as VARCHAR2.

      With that declared, Oracle automatically applies datatype conversion, where necessary, and possible. So to process this

      COLUMN = 27

      So… 27 (INT) is automatically converted to “27” (varchar2), which is then stored in COLUMN.

      One must be aware of when type conversions automatically happen in Oracle.

  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.

    1. (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.

    2. 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.

    3. Try var_dump( (int) “hello” );
      You will receive 0
      0 is also false where as “hello” would equate to true.
      This is where you lose your original definition and could potentially break an algorithm.

      A good example of this would be:

      function getPercentage() {
      $temp = func_get_args();
      if( is_numeric( $temp[0] ) ) {
      $myValue = (int) $temp[0];
      if( is_string( $temp[0] ) && !is_numeric( $temp[0] ) ) {
      $myValue = $temp[0];
      if( is_int($myValue) ){
      print ($myValue * 100) . "%";
      //had we done (int) to the string we would have done 0 * 100
      print $myValue;
      //For some reason the percentage was already entered.

      It’s also very handy for date functions.
      Keeping track of numeric, string, and integer values would be also needed.
      if today where Monday, date(“N”) would be “1” not 1.

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

    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.

  7. $var = “123456”;
    $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 is true.

    For example false is a boolean value, whereas “false” is not. It is a string that would evaluate to true.
    You would not want a string mixed with the boolean type, the same applies to integers and strings.
    Try this
    if ( false ) {
    print “You’ll never see this”;
    if ( true ) {
    print “Hello World”;
    if ( “false” ) {
    print “If I were actually false this line shouldn’t be here”;

  8. If you used is_numeric() on everything that holds numbers then you wouldbe missing an important factor that could lead to serious problems.

    is_numeric looks for strings holding numbers.
    is_int() looks for integers

    integers can do math and be iterated with, strings cant.

    This is not a bug with PHP, this is a lack of a fundamental programming skill.

    While I dont like Java, learning java would burn this concept into ones brain bc of its variable type instantiation requirement.

  9. if someone want to inform users instead of converting numbers I found a trick


    $add_val=($add_val-$add_val)+$add_val; // trick

    echo “its not int”;

  10. I wasted nearly an hour on this, wish I had have found the page sooner. Still getting confused with all this int stuff!

Leave a Reply

%d bloggers like this:

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.