195

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
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Amandasaurus
  • 58,203
  • 71
  • 188
  • 248

31 Answers31

222

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);
}
Dominic Rodger
  • 97,747
  • 36
  • 197
  • 212
  • 2
    @Anurag, edited in (though if you really care about this stuff, a regex is probably simpler). – Dominic Rodger Jan 06 '10 at 11:14
  • 5
    If you want to check for negative integers, add the use of **abs()** like so `ctype_digit(abs($str))` instead of just `ctype_digit`. – enchance Jun 13 '12 at 20:10
  • 3
    @enchance `abs('definitelynotanint') === 0`. moreover, `ctype_digit` takes only strings – Klesun Jun 08 '16 at 16:16
  • 1
    This is not correct. A large integer in PHP is a double! – jgmjgm Dec 11 '17 at 18:47
  • Note that this will detect strings with leading zeros as integer as well like for example phone numbers and zip codes that may start with a 0 and should not be handled as integer values. – Rico May 07 '21 at 09:31
124

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);
}
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • 3
    I like most of the answers, but I think this one is the most elegant. – Ian Dunn Aug 20 '11 at 19:09
  • 6
    @grenoult https://3v4l.org/qVRRS gives int 0 for strings and integers, so not sure why you think it doesnt work. Are you comparing the result with `==` maybe? – Gordon Nov 04 '15 at 10:58
  • 1
    @Gordon my mistake, I was indeed comparing with only a `if` https://3v4l.org/IAvCF – Guillaume Renoult Nov 05 '15 at 01:10
  • This will do some strange things such as return true for "+123". It is mitigated by that it's meant to return the true int but it still might not be what people want which is a strict int check. – jgmjgm Dec 11 '17 at 18:09
  • @MohammedRédaOUASSINI the OP asked to validate *strings*, not floats. It *will* work if you pass in '-1.0' (a string) It will *not* work if you pass in -1.0 (a float), but that wasn't a requirement to start with. See https://3v4l.org/bOX6X – Gordon Feb 11 '20 at 06:45
  • Yes, you're right, I was trying to create a simple function that validates all types of entries. Just in case someone is interested, here is the function: `function isInt($value) { if(!\is_int($value) && !\is_string($value)) { return false; } return \ctype_digit(\sprintf('%s', $value)); }` – Mohammed Réda OUASSINI Feb 12 '20 at 08:38
19

+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
nickf
  • 537,072
  • 198
  • 649
  • 721
  • 2
    +1 to you too (though I wonder if that particular case would be handled more clearly by trimming the input string). – Dominic Rodger Jan 06 '10 at 10:38
  • 8
    This method doesn't work for non-numeric strings: is_int("abc"+0) is true – Kai Pommerenke Jan 29 '13 at 18:07
  • 1
    True, it does not work for non-numeric strings... what a about `is_int($inty + 0) && is_numeric($inty)` (I know it is a lazy solution but works for most intents and purposes... – Alex Mantaut Jul 14 '17 at 00:28
  • Using type jugging will result in quite loose checking where ints can be represented in all kinds of string ways, it's not a string check. You also do not need to +0. You can simply +$var. – jgmjgm Dec 11 '17 at 18:10
  • This very answer proves being wrong on " 2" (as well as any other string contains non-numeric chars). And still... +20? How it's even possible? – Your Common Sense May 12 '23 at 13:46
13

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;
}
greg
  • 665
  • 3
  • 5
11

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
Jonathan Gagne
  • 4,241
  • 5
  • 18
  • 30
  • This will fail on integers with leading zeros, e.g. `05` – etuardu Nov 23 '21 at 10:18
  • Because I do not consider `05` as an integer such as the value `five` as well. Please read this post to understand my opinion. https://stackoverflow.com/questions/33444219/how-to-read-an-integer-that-starts-with-zero/33444275 – Jonathan Gagne Nov 24 '21 at 15:54
  • If you are parsing user input, especially inserted in tabular form, it's common to find leading zeros used for padding (`001`, `002`, …, `075`, `125`). That's why I wanted to point it out. – etuardu Nov 25 '21 at 16:25
9

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
Costa
  • 4,851
  • 33
  • 30
8
/**
 * 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.

rybo111
  • 12,240
  • 4
  • 61
  • 70
GmonC
  • 10,924
  • 1
  • 30
  • 38
  • rather than scatter my code with little utility functions, I'd rather have something that's built into php. – Amandasaurus Jan 06 '10 at 10:36
  • I don't like these hacks either. But using this approach or ctype suggestion by Dominic, you're going to encapsulate all implementation anyway in a method. When using php, I always have an "Util" class to address these problems. – GmonC Jan 06 '10 at 13:53
  • `elseif` can just be `if` because you've already returned in the statement above it. – rybo111 Jun 08 '15 at 08:39
  • Also no need to return false at the end. – rybo111 Jun 08 '15 at 09:25
  • 1
    If `$data` is an object, this will return `null`. Better have the `return false;` at the end. – Theodore R. Smith May 08 '19 at 13:59
5
function my_is_int($var) {
    return preg_match('/^\d+$/', $var);
}
4

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)
Jordi Martínez
  • 358
  • 3
  • 14
  • is_numeric() was what I was looking for. – ccjjmartin Sep 26 '17 at 22:16
  • 1
    To be sure this is what you want, you'll need to read the PHP source as it's not well documented. For example, is_numeric accepts leading whitespace, which is not something that's documented in the PHP manual. You should also keep in mind that string integers that are too large to be represented with a native int are converred to float. – jgmjgm Dec 11 '17 at 19:00
4

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);
}
vietbq
  • 41
  • 2
  • 1
    When answering an old question, your answer would be much more useful to other StackOverflow users if you included some context to explain how your answer helps, particularly for a question that already has an accepted answer. See: [How do I write a good answer](https://stackoverflow.com/help/how-to-answer). – David Buck Dec 21 '19 at 18:37
2

I´m using this one:

function isInt($val){

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

}

var_dump (isInt("1"));
julesdude
  • 195
  • 1
  • 2
  • 16
2
function my_is_int($var){
    return is_numeric($var) && gettype($var+0)=='integer';
}
grant sun
  • 120
  • 5
2

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)
Smuuf
  • 6,339
  • 3
  • 23
  • 35
2

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.

Pikamander2
  • 7,332
  • 3
  • 48
  • 69
1

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

Jack M.
  • 3,676
  • 5
  • 25
  • 36
1

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
wolakpaul
  • 27
  • 2
  • I don't understand why this would return false for 234- or --234, did you test it? This will return true for -----1234---- – jgmjgm Dec 11 '17 at 18:29
1

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
mwallisch
  • 1,751
  • 2
  • 21
  • 29
1

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
SharkWipf
  • 384
  • 1
  • 4
  • 12
  • very nice. if you wanted to handle the numeric string with negative sign, you could change to ctype_digits(preg_replace('/^-/', '', $s)), though that starts to get a bit long-winded. also, 0x101010 is an int, so that is not an error, but rather "0x101010" might be considered incorrect, and I'm sure binary strings matching binary literal notation ("0b11111") would fail as well – fbas May 01 '18 at 20:50
1

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.

jgmjgm
  • 4,240
  • 1
  • 25
  • 18
1

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';
}
?>
EnryBi
  • 21
  • 6
1

I think is simpler than use ctype_digit and work with mixed

function isNumericInt(mixed $value): bool
{
    if (is_int($value)) {
        return true;
    }
    if (is_string($value)) {
        // if '' should give false
        return 1 === preg_match('/^[0-9]+$/', $value);

        // if '' should gives true
        return 1 === preg_match('/^[0-9]*$/', $value);
    }

    return false;
}
0

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

0

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.

Dragonaire
  • 313
  • 3
  • 7
0
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;
}
Volker
  • 49
  • 5
  • It's missing a check that string !== ''. If you put a really long integer into this it will also return true for something PHP would cast to a float! – jgmjgm Dec 11 '17 at 19:09
0

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";
}
Saman
  • 5,044
  • 3
  • 28
  • 27
0

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';
}
N3K0100183
  • 30
  • 6
0

You can do that

function isNumericInt(mixed $value): bool {

    return is_int($value) || ctype_digit(strval(abs($value)));

}
rplaurindo
  • 1,277
  • 14
  • 23
-1

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

Paolo
  • 22,188
  • 6
  • 42
  • 49
  • This is the only answer that recognises locale changes also changes how PHP interprets and outputs numbers. Be very careful with setlocale and solutions that make assumptions about separators. – jgmjgm Dec 11 '17 at 19:11
-1

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.

Joe
  • 7
  • 2
-1

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");
}
...
Mohd Abdul Mujib
  • 13,071
  • 8
  • 64
  • 88
-1
$int_check=(int)(trim($val));   // returning 0 for non int value
if($int_check<=0){
    // do for non int 
}
else 
{
    `enter code here`//integer
}
James Risner
  • 5,451
  • 11
  • 25
  • 47
Soumen Das
  • 13
  • 1