How to check that a string is an int, but not a double, etc.?

PhpStringCastingTypesInt

Php Problem Overview


PHP has an intval() function that will convert a string to an integer. However I want to check that the string is an integer beforehand, so that I can give a helpful error message to the user if it's wrong. PHP has is_int(), but that returns false for string like "2".

PHP has the is_numeric() function, but that will return true if the number is a double. I want something that will return false for a double, but true for an int.

e.g.:

my_is_int("2") == TRUE
my_is_int("2.1") == FALSE

Php Solutions


Solution 1 - Php

How about using ctype_digit?

From the manual:

<?php
$strings = array('1820.20', '10002', 'wsl!12');
foreach ($strings as $testcase) {
    if (ctype_digit($testcase)) {
        echo "The string $testcase consists of all digits.\n";
    } else {
        echo "The string $testcase does not consist of all digits.\n";
    }
}
?>

The above example will output:

The string 1820.20 does not consist of all digits.
The string 10002 consists of all digits.
The string wsl!12 does not consist of all digits.

This will only work if your input is always a string:

$numeric_string = '42';
$integer        = 42;

ctype_digit($numeric_string);  // true
ctype_digit($integer);         // false

If your input might be of type int, then combine ctype_digit with is_int.

If you care about negative numbers, then you'll need to check the input for a preceding -, and if so, call ctype_digit on a substr of the input string. Something like this would do it:

function my_is_int($input) {
  if ($input[0] == '-') {
    return ctype_digit(substr($input, 1));
  }
  return ctype_digit($input);
}

Solution 2 - Php

filter_var should do it:

var_dump(filter_var('2', FILTER_VALIDATE_INT));   // 2
var_dump(filter_var('2.0', FILTER_VALIDATE_INT)); // false
var_dump(filter_var('2.1', FILTER_VALIDATE_INT)); // false

but

var_dump(filter_var(2, FILTER_VALIDATE_INT));     // 2
var_dump(filter_var(2.0, FILTER_VALIDATE_INT));   // 2
var_dump(filter_var(2.1, FILTER_VALIDATE_INT));   // false

If you just want Booleans as return values, wrap it into a function, e.g.

function validatesAsInt($number)
{
    $number = filter_var($number, FILTER_VALIDATE_INT);
    return ($number !== FALSE);
}

Solution 3 - Php

+1 to Dominic's answer (using ctype_digit). Another way you could do it is with type coercion:

$inty = "2";
$inty2 = " 2";
$floaty = "2.1";
$floaty2 = "2.0";

is_int($inty + 0); // true
is_int($floaty + 0); // false
is_int($floaty2 + 0); // false

// here's difference between this and the ctype functions.
is_int($inty2 + 0);  // true
ctype_digit($inty2); // false

Solution 4 - Php

Cast it to int. if it still have the same value its int;

function my_is_int($var) {
  $tmp = (int) $var;
  if($tmp == $var)
       return true;
  else
       return false;
}

Solution 5 - Php

One really clean way that I like to use is that one. You cast it twice, first in int, secondly in string, then you strict compare ===. See the example below:

$value === (string)(int)$value;

Now, about your function my_is_int, you can do something like this:

function my_is_int($value){ return $value === (string)(int)$value; }
my_is_int('2');  // true
my_is_int('2.1') // false

Solution 6 - Php

Had a need for a robust is_int recently. I found intval() too unpredictable:

intval(array('foo', 'bar')) //returns 1 ?!?
intval("2dog") //returns 2 even though the value is definitely not an integer
intval("dog2") //also returns 2


Came across this snippet in the PHP documentation comments, and after testing it, it covers almost everything you throw at it:

function my_is_int($s) {
    return (is_numeric($s) ? intval($s) == $s : false);
}


my_is_int(2); //true
my_is_int("2"); //true
my_is_int(2.1); //false
my_is_int("2.1"); //false
my_is_int("dog"); //false
my_is_int("2dog"); //false
my_is_int("dog2"); //false
my_is_int(array('foo', 'bar')); //false
my_is_int(array(1)); //false


But careful:

my_is_int(2.0); //true
my_is_int("2.0"); //true

Solution 7 - Php

/**
 * Check if a number is a counting number by checking if it
 * is an integer primitive type, or if the string represents
 * an integer as a string
 */
function is_int_val($data) {
	if (is_int($data) === true) return true;
	if (is_string($data) === true && is_numeric($data) === true) {
		return (strpos($data, '.') === false);
	}
}

Source.

Solution 8 - Php

function my_is_int($var) {
    return preg_match('/^\d+$/', $var);
}

Solution 9 - Php

I´m using this one:

function isInt($val){

	return (filter_var($val, FILTER_VALIDATE_INT) !== false && strpos($val, '-') === false);
	
}

var_dump (isInt("1"));

Solution 10 - Php

I devised a way I couldn't find anywhere, so I'm putting it in here:

Without further ado it's this: ctype_digit((string) abs($input))

Example:

function means_int($input) {
	return ctype_digit((string) abs($input));
}

$list = array(
	0,
	'0',
	1,
	'1',
	1.1,
	'1.1',
	2.0,
	'2.0',	
	2.6,
	'2.6',
	-4,
	'-4',	
	-3.2,
	'-3.2',
	-30.02,
	'-30.02',	
	100.00,
	'100.00',	
);

foreach ($list as $x) {
	var_dump($x);
	var_dump(means_int($x));
	echo PHP_EOL;
}

Results: (are as expected, I suppose)

int(0)
bool(true)

string(1) "0"
bool(true)

int(1)
bool(true)

string(1) "1"
bool(true)

float(1.1)
bool(false)

string(3) "1.1"
bool(false)

float(2)
bool(true)

string(3) "2.0"
bool(true)

float(2.6)
bool(false)

string(3) "2.6"
bool(false)

int(-4)
bool(true)

string(2) "-4"
bool(true)

float(-3.2)
bool(false)

string(4) "-3.2"
bool(false)

float(-30.02)
bool(false)

string(6) "-30.02"
bool(false)

float(100)
bool(true)

string(6) "100.00"
bool(true)

Solution 11 - Php

You can just check for a number, if it is then check than casting is given a double or not:

((is_numeric($var) && !is_double(1*$var)));

Just for positive numbers:

(is_numeric($var) && !is_double(1*$var)) && ($var >= 0)

Checking it:

$numbersToCheck = array("a", "-1", "1", "1.0", "1.2");

foreach ($numbersToCheck as $var) {
    echo $var . " is integer? ";var_dump((is_numeric($var) && !is_double(1*$var)));

    echo $var . " is a positive integer? ";var_dump((is_numeric($var) && !is_double(1*$var)) && ($var >= 0));
}

Output:

a is integer? bool(false)
a is a positive integer? bool(false)
-1 is integer? bool(true)
-1 is a positive integer? bool(false)
1 is integer? bool(true)
1 is a positive integer? bool(true)
1.0 is integer? bool(false)
1.0 is a positive integer? bool(false)
1.2 is integer? bool(false)
1.2 is a positive integer? bool(false)

Solution 12 - Php

Try this:

$string='12abc';
if ((int)$string==$string) var_dump((int)$string); else echo 'Invalid!';
// Outputs: Invalid!

$string='789';
if ((int)$string==$string) var_dump((int)$string); else echo 'Invalid!';
// Outputs: int 789

$string='345.00';
if ((int)$string==$string) var_dump((int)$string); else echo 'Invalid!';
// Outputs: 345

$string='123.01';
if ((int)$string==$string) var_dump((int)$string); else echo 'Invalid!';
// Outputs: Invalid!

Also works if your $string has decimal places

Solution 13 - Php

This will take care of negative number as well

function myIsInt()
{
   return (is_numeric($var) AND (is_int($var) OR ctype_digit(trim($var, '-'))))
}
//'234-' => false
//'-234' => true
//'--234' => false
//'234' => true

Solution 14 - Php

function my_is_int($var){
    return is_numeric($var) && gettype($var+0)=='integer';
}

Solution 15 - Php

Maybe not the most performant way of doing it. But you can write it in one line.

function my_is_int($input) {
    return intval($input).'' === $input.'';
}

As expected:

    my_is_int(1);     // TRUE
    my_is_int(-1);    // TRUE
    my_is_int(1.2);   // FALSE
    my_is_int("1");   // TRUE
    my_is_int("-1");  // TRUE
    my_is_int("1.2"); // FALSE
    my_is_int(0);     // TRUE
    my_is_int(null);  // FALSE

Gotcha:

    my_is_int(1.0);   // TRUE
    my_is_int("1.0"); // FALSE

Solution 16 - Php

A few years late, but based on the answers given here I came up with a solution that's slightly more accurate (on booleans, in particular) and more efficient (I think) than most other answers:

function my_is_int($s) {
    return ctype_digit($s) || is_int($s);
}

Working as expected for these:

my_is_int(2);                   // true
my_is_int("2");                 // true
my_is_int(-2);                  // true
my_is_int(2.0);                 // false
my_is_int("2.0");               // false
my_is_int(2.1);                 // false
my_is_int("2.1");               // false
my_is_int("dog");               // false
my_is_int("2dog");              // false
my_is_int("dog2");              // false
my_is_int(array('foo', 'bar')); // false
my_is_int(array(1));            // false
my_is_int(true);                // false
my_is_int(false);               // false
my_is_int("true");              // false
my_is_int("false");             // false
my_is_int("0x101010");          // false

Except maybe for these 2:

my_is_int(0x101010);            // true
my_is_int("-2");                // false

Solution 17 - Php

Here's a simple solution that uses is_numeric, floatval, and intval:

function is_string_an_int($string)
{
    if (is_string($string) === false)
    {
        //throw some kind of error, if needed
    }

    if (is_numeric($string) === false || floatval(intval($string)) !== floatval($string))
    {
        return false;
    }
    
    else
    {
        return true;
    }
}

Results:

is_string_an_int('-1'); //true
is_string_an_int('-1.0'); //true
is_string_an_int('-1.1'); //false
is_string_an_int('0'); //true
is_string_an_int('0.0'); //true
is_string_an_int('0.1'); //false
is_string_an_int('1'); //true
is_string_an_int('1.0'); //true
is_string_an_int('1.1'); //false
is_string_an_int('' . PHP_INT_MAX); //true
is_string_an_int('foobar'); //false
is_string_an_int('NaN'); //false
is_string_an_int('null'); //false
is_string_an_int('undefined'); //false

Note that values greater than PHP_INT_MAX may return false.

Solution 18 - Php

It works perfectly! Hope it will be helpful to you =)

$value = 1; // true
$value = '1'; // true
$value = '1.0'; // true
$value = ' 1'; // true
$value = '01'; // true

$value = -1; // true
$value = '-1'; // true
$value = '-1.0'; // true
$value = ' -1'; // true
$value = '-01'; // true

$value = PHP_INT_MIN; // true
$value = PHP_INT_MAX; // true
$value = 0x000001; // true

$value = 'a'; // false
$value = '1a'; // false
$value = 'a1'; // false
$value = 1.1; // false
$value = '1.1'; // false
$value = '--1'; // false
$value = []; // false
$value = [1,2]; // false
$value = true; // false
$value = false; // false
$value = null; // false
$value = 'NaN'; // false
$value = 'undefined'; // false

function isInt($value) {
    return is_numeric($value) && floatval(intval($value)) === floatval($value);
}

Solution 19 - Php

How about:

function isIntStr($str) {
   return preg_match('/^(-?\d+)(?:\.0+)?$/', trim($str), $ms)
       && bcComp($ms[1], PHP_INT_MAX) <= 0
       && bcComp($ms[1], -PHP_INT_MAX - 1) >= 0;
}

This function should only return true for any string number that can be cast to int with (int) or intval() without losing anything of mathematical significance (such as non-zeros after decimal point or numbers outside of PHP's integer range) while accepting things that aren't mathematically significant (such as whitespace; leading zeros; or, after the decimal point, zeros exclusively).

It will return false for '10.' but not for '10.0'. If you wanted '10.' to be true you could change the + after the 0 in the regular expression to *.

Solution 20 - Php

Here some code I've used that seems to work well and doesn't have any of the issues that many of the others do.

if (0 != strlen(str_replace(range(0, 9), '', $TestInt))) { print 'Not an integer!';}

It does not check order etc so not meant for negative integers but with some addition code that can be done as well using some of the other ideas from above. It can also be adapted to work with binary array('0', '1') or Hexadecimals as well etc.

Solution 21 - Php

See this. Converts $val to integer and then checks if the original $val converted to string is IDENTICAL (===) - just == won't work as expected - to the integer val converted to string.

function validInt($val, $min=null, $max=null) {
	$ival = intval($val);
	//echo "'$ival' '$val'<br>\n"; // Uncomment to see the comparisons done in below if block
	if(''.$ival !== ''.$val) {
		return false;
	}
	if($min !== null && $ival < $min)
		return false;
	if($max !== null && $ival > $max)
		return false;
	return true;
}

If you don't check string values it might not work as you expect it:

$nums = array(
	'1',
	'+1',
	'-1',
	'01',
	'1.0',
	'.0',
	'1.123',
	'a123',
	'0x101010',
	1,
	-1,
	01,
	1.0,
	.0,
	1.123,
	0x101010,
);
foreach($nums as $num) {
	if(validInt2($num))
		echo $num." - Valid integer.<br>\n";
	else
		echo $num." - Not a valid integer.<br>\n";
}

Output:

1 - Valid integer.
+1 - Not a valid integer.
-1 - Valid integer.
01 - Not a valid integer.
1.0 - Not a valid integer.
.0 - Not a valid integer.
1.123 - Not a valid integer.
a123 - Not a valid integer.
0x101010 - Not a valid integer.
1 - Valid integer.
-1 - Valid integer.
1 - Valid integer.
1 - Valid integer.
0 - Valid integer.
1.123 - Not a valid integer.
1052688 - Valid integer.

Reason being even if you use hex (0x101010), octal (01) or an integer stored as float (1.0, 0.0), internally all are stored as float. However, if you use the function to check for int stored as a string, it will work.

Solution 22 - Php

public static function isNumeric($value, $negativ = false) {
	return is_int($value) || is_string($value) && (
		ctype_digit($value) || (
			$negativ && $value{0} == '-' && ctype_digit(substr($value, 1))
		)
	);

	//alternativ:
	//return $value == (int) $value;
}

Solution 23 - Php

You can use the following condition. Notice that you should not use !==

$value = 12; // true
$value = '12'; // true
$value = 'abc'; // false
$value = 12.1; // false
$value = '12.1'; // false

if (!is_numeric($value) || (int) $value != (float) $value) {
    echo "false";
} else {
    echo "true";
}

Solution 24 - Php

Here is a working 1 row example :

<?php
// combination of is_int and type coercion
// FALSE: 0, '0', null, ' 234.6',  234.6
// TRUE: 34185, ' 34185', '234', 2345
$mediaID =  34185;
if( is_int( $mediaID + 0) && ( $mediaID + 0 ) > 0 ){
    echo 'isint'.$mediaID;
}else{
    echo 'isNOTValidIDint';
}
?>

Solution 25 - Php

If you want to genuinely know if a string is a valid representation of a true PHP integer type...

in_array($string, array_map('strval', range(PHP_INT_MIN, PHP_INT_MAX)), true)

However this is impossible to run as the set is too large (will not fit in memory in this case, if you loop instead it will take too many CPU cycles).

You can perhaps do a binary search with string comparison, however there are better ways.

The simplest being:

strlen($string) <= max(strlen((string)PHP_INT_MIN), strlen((string)PHP_INT_MAX)) && $string === (string)(int)$string

There are some other unusual ways to approach it such as:

is_int(array_keys([$string => null])[0])

You can also do string comparison but you'll still need to do things such as ctype_digit, check the length is reasonable (don't waste CPU before doing things like ctype_digit) and have some awkward handling for negative numbers.

Note that filter_var does not correctly assert that a string is genuinely the representation of a PHP integer. It will allow a leading + and surrounding whitespace.

Internally PHP uses the function "_zend_handle_numeric_str" for strict comparison but it doesn't directly expose this anywhere, hence the trick using the array keys (which does use it to convert any string that's a representation of a PHP integer to a PHP integer).

If you want binary safe conversion to and from PHP this is the approach to take.

Not everyone might want that and it might be a case of handling user input. filter_var isn't too bad for that and will be fairly safe in most cases for people new to PHP.

A length check, ctype_digit and then a check of converted value that it's in a range is also fairly solid for user input. More complex schemes might want trim or regex.

The problem with a lot of the answers here in that respect is that while the question is vague, the answers shouldn't be. If you're going to propose a solution you should be able to explain exactly what it will and wont expect. Without that there's no telling if an answer matches a question or is safe. The PHP manual does not always help because it doesn't explain all of the caveats for each of the relevant methods it supplies. Things such as ctype_digit and is_int are very reliable and easy to predit but the specifics of is_numeric, filter_var and juggling (+$var) or casting (intval/floatval) are poorly documented.

This is PHP fudge for you. It has a myriad number of schemas for interpreting strings as integers, with inconsistencies. The strictest method of validating an integer string is not directly exposed to the user.

Solution 26 - Php

If you are handling numeric IDs from mysql and are trying to impose a validation for it to be a valid non zero int or an int but in string format (as mysql always returns everything in string format) and not NULL. You can use the following. This is the most error/warning/notice proof way to only allow int/string-ints which I found.

...
if(empty($id) || !is_scalar($id) || !ctype_digit($id)){
  throw("Invalid ID");
}
...

Solution 27 - Php

A simple way is to cast the number to both an integer and a float and check if they are the same

if ((int) $val == (float) $val) {
    echo 'AN INTEGER';
} else {
    echo 'NOT AN INTEGER';
}

Solution 28 - Php

Could either use is_numeric() then check for presence of "." in the string (not particularly culture-sensitive though).

Alternatively use is_numeric() then cast to a double and see if $var == floor($var) (should return true if it's an integer).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAmandasaurusView Question on Stackoverflow
Solution 1 - PhpDominic RodgerView Answer on Stackoverflow
Solution 2 - PhpGordonView Answer on Stackoverflow
Solution 3 - PhpnickfView Answer on Stackoverflow
Solution 4 - PhpgregView Answer on Stackoverflow
Solution 5 - PhpJonathan GagneView Answer on Stackoverflow
Solution 6 - PhpCostaView Answer on Stackoverflow
Solution 7 - PhpGmonCView Answer on Stackoverflow
Solution 8 - PhpMichael ConnorView Answer on Stackoverflow
Solution 9 - PhpjulesdudeView Answer on Stackoverflow
Solution 10 - PhpSmuufView Answer on Stackoverflow
Solution 11 - PhpJordi MartínezView Answer on Stackoverflow
Solution 12 - PhpJack M.View Answer on Stackoverflow
Solution 13 - PhpwolakpaulView Answer on Stackoverflow
Solution 14 - Phpgrant sunView Answer on Stackoverflow
Solution 15 - PhpmwallischView Answer on Stackoverflow
Solution 16 - PhpSharkWipfView Answer on Stackoverflow
Solution 17 - PhpPikamander2View Answer on Stackoverflow
Solution 18 - PhpvietbqView Answer on Stackoverflow
Solution 19 - Phpuser2923827View Answer on Stackoverflow
Solution 20 - PhpDragonaireView Answer on Stackoverflow
Solution 21 - PhpJoeView Answer on Stackoverflow
Solution 22 - PhpVolkerView Answer on Stackoverflow
Solution 23 - PhpSamanView Answer on Stackoverflow
Solution 24 - PhpEnryBiView Answer on Stackoverflow
Solution 25 - PhpjgmjgmView Answer on Stackoverflow
Solution 26 - PhpMohd Abdul MujibView Answer on Stackoverflow
Solution 27 - PhpN3K0100183View Answer on Stackoverflow
Solution 28 - PhpPaoloView Answer on Stackoverflow