Checking if a string contains an integer

Dont want to accidently turn Jhong's answer into a CW, so for the record here is the results when testing with === instead of ==.

function test($var) {
    return ((string)(int)$var === $var);
}

var_dump( test(1)             ); // returns false vs TRUE
var_dump( test('1')           ); // returns TRUE
var_dump( test('1.0')         ); // returns false vs TRUE
var_dump( test('1.1')         ); // returns false 
var_dump( test('0xFF')        ); // returns false
var_dump( test('0123')        ); // returns false vs TRUE
var_dump( test('-0123')       ); // returns false vs TRUE
var_dump( test('-1000000')    ); // returns TRUE
var_dump( test('+1000000')    ); // returns false vs TRUE
var_dump( test('2147483648')  ); // returns false
var_dump( test('-2147483649') ); // returns false

if((string)(int)$var == $var) {
    echo 'var is an integer or a string representation of an integer';
}

Example results:

var_dump( test(1)             ); // TRUE
var_dump( test('1')           ); // TRUE
var_dump( test('1.0')         ); // TRUE
var_dump( test('1.1')         ); // false
var_dump( test('0xFF')        ); // false
var_dump( test('0123')        ); // TRUE
var_dump( test('01090')       ); // TRUE
var_dump( test('-1000000')    ); // TRUE
var_dump( test('+1000000')    ); // TRUE
var_dump( test('2147483648')  ); // false
var_dump( test('-2147483649') ); // false

See Gordon's answer below for how this would behave differently if === were used for comparison instead of ==.


Update Since PHP 7.1 there are problems with using is_int() with non-numeric values, as discussed in this SO Answer. In any case, this is a very old answer and I'd really view it as something of a hack at this point so YMMV ;)

Sorry if this question has been answered but this has worked for me in the past:

First check if the string is_numeric. if it is add a 0 to the value to get PHP to covert the string to its relevant type. Then you can check if it's an int with is_int. Quick and dirty but it works for me...

$values = array(1, '2', '2.5', 'foo', '0xFF', 0xCC, 0644, '0777');

foreach ($values as $value) {
  $result = is_numeric($value) && is_int(($value + 0)) ? 'true' : 'false';
  echo $value . ': ' . $result . '<br />';
}

Results:

1: true
2: true
2.5: false
foo: false
0xFF: true
204: true
420: true
0777: true

The only problem is that it will evaluate octal values wrapped in a string literally, i.e: '0123' will simply become 123. But that's easy to address :)


Not the fastest method, but filter_var() is quite accurate:

function test($s)
{
    return filter_var($s, FILTER_VALIDATE_INT) !== false;
}

Here are the results based on Jhong's answer, differences marked with !!:

var_dump(test(1)            ); // true
var_dump(test('1')          ); // true
var_dump(test('1.0')        ); // false !!
var_dump(test('1.1')        ); // false
var_dump(test('0xFF')       ); // false
var_dump(test('0123')       ); // false !!
var_dump(test('01090')      ); // false !!
var_dump(test('-1000000')   ); // true
var_dump(test('+1000000')   ); // true
var_dump(test('2147483648') ); // true !! on 64bit
var_dump(test('-2147483649')); // true !! on 64bit

To allow octal integers:

function test($s)
{
   return filter_var($s, FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_OCTAL) !== false;
}

Results:

var_dump(test('0123') ); // true
var_dump(test('01090')); // false !!

To allow hexadecimal notation:

function test($s)
{
   return filter_var($s, FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_HEX) !== false;
}

Results:

var_dump(test('0xFF')); // true !!

Tags:

Php

Validation