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

31

199

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
Trimerous answered 6/1, 2010 at 10:21 Comment(4)
how should "2.0" be treated?Ladino
(string)(int)$value === $valueSirois
yeah, whats the deal with is_int?Sundry
@DanielDolz is_int simply tests whether a value is of integer data type. Therefore, it will return false for any string, regardless of whether it passes off as a representation of an integer.Mahone
S
225

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);
}
Scourge answered 6/1, 2010 at 10:29 Comment(5)
@Anurag, edited in (though if you really care about this stuff, a regex is probably simpler).Scourge
If you want to check for negative integers, add the use of abs() like so ctype_digit(abs($str)) instead of just ctype_digit.Bryophyte
@Bryophyte abs('definitelynotanint') === 0. moreover, ctype_digit takes only stringsFalange
This is not correct. A large integer in PHP is a double!Polysyllabic
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.Divisor
S
127

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);
}
Snout answered 6/1, 2010 at 10:38 Comment(6)
I like most of the answers, but I think this one is the most elegant.Levanter
@grenoult 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?Snout
@Snout my mistake, I was indeed comparing with only a if 3v4l.org/IAvCFIndicia
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.Polysyllabic
@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 3v4l.org/bOX6XSnout
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)); }Feltner
L
20

+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
Ladino answered 6/1, 2010 at 10:34 Comment(5)
+1 to you too (though I wonder if that particular case would be handled more clearly by trimming the input string).Scourge
This method doesn't work for non-numeric strings: is_int("abc"+0) is trueSchooner
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...Scoundrel
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.Polysyllabic
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?Winner
D
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;
}
Dufrene answered 6/1, 2010 at 11:50 Comment(4)
Or even shorter: return $var == (int) $var;Lindane
@AI.G. What happens if the cast failsAbundant
This is very close, should avoid type juggling though if not needed. This will work if $var is "123xxxx".Polysyllabic
Similar to the comment above, this will fail for my_is_int('0.0'), which will return true while it should probably return false.Clarsach
C
12

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
Cornflower answered 14/8, 2018 at 18:21 Comment(3)
This will fail on integers with leading zeros, e.g. 05Married
Because I do not consider 05 as an integer such as the value five as well. Please read this post to understand my opinion. #33444719Cornflower
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.Married
S
9
/**
 * 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.

Starlin answered 6/1, 2010 at 10:28 Comment(5)
rather than scatter my code with little utility functions, I'd rather have something that's built into php.Trimerous
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.Starlin
elseif can just be if because you've already returned in the statement above it.Fortnightly
Also no need to return false at the end.Fortnightly
If $data is an object, this will return null. Better have the return false; at the end.Kingbolt
I
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
Indaba answered 10/3, 2013 at 22:33 Comment(1)
From your code I got it: $idInt = (int)$id; $isInt = $idInt > 0 && (string)$idInt === $id;Nonu
O
6

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);
}
Osteo answered 21/12, 2019 at 18:18 Comment(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.Boatright
B
5
function my_is_int($var) {
    return preg_match('/^\d+$/', $var);
}
Birck answered 16/5, 2012 at 12:51 Comment(2)
I used a variation of this, allowing the number signal: '/^[-+]?[0-9]+$/'Paroxysm
This is the same as ctype_digit, it also allows a trailing newline.Polysyllabic
A
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)
Amata answered 20/1, 2016 at 9:5 Comment(2)
is_numeric() was what I was looking for.Thrombus
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.Polysyllabic
M
2

I´m using this one:

function isInt($val){

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

}

var_dump (isInt("1"));
Magdala answered 19/8, 2013 at 8:8 Comment(0)
S
2
function my_is_int($var){
    return is_numeric($var) && gettype($var+0)=='integer';
}
Selwin answered 14/10, 2014 at 6:14 Comment(1)
Clean and simple!Boyes
O
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)
Outface answered 21/4, 2015 at 9:41 Comment(1)
abs is casting input from string to numeric. It's the same as is_int(+$var).Polysyllabic
S
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.

Squad answered 22/7, 2019 at 7:25 Comment(0)
H
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

Hurlbut answered 26/6, 2014 at 7:52 Comment(1)
This will treat 123xxxx as a valid int string.Polysyllabic
V
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
Vulturine answered 3/9, 2014 at 16:1 Comment(1)
I don't understand why this would return false for 234- or --234, did you test it? This will return true for -----1234----Polysyllabic
L
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
Lavonlavona answered 12/2, 2016 at 20:52 Comment(1)
This one is correct, as long as you ensure $input is of type string.Polysyllabic
L
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
Laveta answered 6/4, 2017 at 0:30 Comment(1)
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 wellEmbrey
P
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.

Polysyllabic answered 11/12, 2017 at 16:9 Comment(0)
T
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';
}
?>
Terse answered 19/1, 2022 at 19:23 Comment(0)
G
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;
}
Galvanize answered 9/5, 2023 at 11:52 Comment(0)
E
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 *.

Electrolyze answered 11/11, 2013 at 9:52 Comment(0)
E
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.

Encyst answered 18/12, 2013 at 4:52 Comment(1)
This is just a very expensive ctype_digit.Polysyllabic
L
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;
}
Luting answered 17/4, 2014 at 14:21 Comment(1)
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!Polysyllabic
D
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";
}
Dorotea answered 3/2, 2015 at 0:9 Comment(0)
F
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';
}
Fiji answered 4/5, 2022 at 17:7 Comment(0)
M
0

You can do that

function isNumericInt(mixed $value): bool {

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

}
Monocotyledon answered 12/5, 2023 at 15:59 Comment(0)
K
-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).

Kline answered 6/1, 2010 at 10:28 Comment(1)
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.Polysyllabic
R
-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.

Riverine answered 19/12, 2013 at 21:34 Comment(0)
R
-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");
}
...
Ruttish answered 25/3, 2019 at 15:19 Comment(0)
T
-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
}
Thigpen answered 25/10, 2022 at 15:22 Comment(1)
Try -1000 or 25 poniesWinner

© 2022 - 2024 — McMap. All rights reserved.