19

I want to check if user input a positive integer number.

1    = true
+10  = true
.1   = false
-1   = false
10.5 = false


Just a positive number. 
No characters.
No special character.
No dot.
No minus sign.

I tried is_int() function but it is returning false even on positive integers. Is there a string to int problem?

Teun Zengerink
  • 4,277
  • 5
  • 30
  • 32
Naveed
  • 41,517
  • 32
  • 98
  • 131
  • *(related)* http://stackoverflow.com/questions/2524680/check-whether-the-string-is-a-unix-timestamp/2524761#2524761 – Gordon Apr 01 '10 at 11:23
  • 3
    what should it return for "+3928742938749283479823749283749" ? – user187291 Apr 01 '10 at 11:24
  • @stereofrog: textbox length validation may block you for this value in my application :) – Naveed Apr 01 '10 at 11:41
  • your likely missing stereofrogs point. A signed `Integer` has a range from -2147483648 to 2147483647. Everything before or after that is technically a `float` – Gordon Apr 01 '10 at 11:45
  • @Gordon: Yes I have got stereofrog's point but I was joking that validation will not allow you to pass integer range :) – Naveed Apr 01 '10 at 11:53

6 Answers6

36

Something like this should work. Cast the value to an integer and compare it with its original form (As we use == rather than === PHP ignores the type when checking equality). Then as we know it is an integer we test that it is > 0. (Depending on your definition of positive you may want >= 0)

$num = "20";

if ( (int)$num == $num && (int)$num > 0 )
Yacoby
  • 54,544
  • 15
  • 116
  • 120
  • 11
    @NAVEED It may be because you are doing `int($num)`. Casting is done by surrounding the type you want to cast to in brackets. In this case `(int)$num`. – Yacoby Apr 01 '10 at 11:25
  • 3
    `(int)$num == $num` `(int)'abcde' == 'abcde'` will return true – Kirzilla Dec 19 '13 at 12:35
  • What if my $num = "0.2" ; then it returns false... How can I check decimals? My numbers are in the following format "+0.2", "-0.8" ect... – Awena Mar 19 '15 at 16:33
  • I solved it this way: `$num = (int)$num >= 0 ? (int)$num : 0;` – Kenny Aug 27 '20 at 14:34
31

Try the native Filter function*

filter_var($value, FILTER_VALIDATE_INT, array(
    'options' => array('min_range' => 1)
));

* if you just want to make sure the input string consists of an arbitrary length digit sequence, use a regex with [0-9] or [\d+]

Examples with filter_var:

var_dump( filter_var(1, FILTER_VALIDATE_INT) ); // int(1)

var_dump( filter_var('1', FILTER_VALIDATE_INT) ); // int(1)

var_dump( filter_var('+10', FILTER_VALIDATE_INT) ); // int(10)

var_dump( filter_var(.1, FILTER_VALIDATE_INT) ); // bool(false)

var_dump( filter_var('.1', FILTER_VALIDATE_INT) ); // bool(false)

var_dump( filter_var(-1, FILTER_VALIDATE_INT, 
    array('options' => array('min_range' => 1))) ); // bool(false)

var_dump( filter_var('-1', FILTER_VALIDATE_INT, 
    array('options' => array('min_range' => 1))) ); // bool(false)

var_dump( filter_var('2147483648', FILTER_VALIDATE_INT) ); // bool(false)

var_dump( filter_var('0xFF', FILTER_VALIDATE_INT) ); // bool(false)

var_dump( filter_var(0xFF, FILTER_VALIDATE_INT) ); // int(255)

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • and what about abcd, 123d, 10.5 and (space) ?? – Naveed Apr 01 '10 at 11:58
  • @NAVEED try yourself and tell me :) I predict all `false` – Gordon Apr 01 '10 at 12:06
  • You have to wait.. leaving ;) – Naveed Apr 01 '10 at 12:08
  • 2
    +1: This is how I'd do it but one thing to keep in mind is that 0 won't validate because PHP automatically casts it into false. Use `===` to avoid automatic type casting in evaluation. – Fake Code Monkey Rashid Sep 14 '11 at 01:52
  • I ran 'abcd', '123d', 10.5, and space (' ') and they all came back as false using simply: ```if (filter_var($int, FILTER_VALIDATE_INT)) { echo("Integer is valid"); } else { echo("Integer is not valid"); }``` – Bryan Miller May 21 '16 at 02:41
  • @user3089840 which is the correct result obviously, since none of the are integers. – Gordon May 21 '16 at 06:53
  • @Gordon Yeah I know - I ran the questions Naveed had posted because I was wondering why he mentioned those, as if they were exceptions and would not work, in case anybody else would like to know that those examples do in fact work right with your method. – Bryan Miller May 23 '16 at 14:29
3

I use a regular expression. Very simple if you think about it. You need more punctuation marks if you want to make a number not a whole positive integer (minus sign and a period). So this just makes sure the only thing you have are numbers 0-9 for the value.

if(!ereg('^[0-9]+$', $value)) {
  $errors .= "This is not a positive whole number";
}

You could add another part on there to make sure it is less than a certain amount of characters as well. Hope this helps.

3

I would say this is the best way

if (is_int($num) && $num > 0)

as typecasting to an int is very slow.

Decko
  • 18,553
  • 2
  • 29
  • 39
  • 2
    But get in mind with this answer that if `$num` is an integer string as `$num = '123'`, `is_int($num)` return `false` – PhoneixS Feb 10 '14 at 11:33
  • 1
    I use `is_numeric()` to check if the given string is a valid number – David Nov 24 '16 at 10:07
1

the easiest way is:

if intval($x) > 0 {
 echo "true"
}
Nopcea Flavius
  • 303
  • 1
  • 3
  • 15
0

if(!preg_match('/^[0-9]+$/', $input)) {

Deprecated: Function ereg() is deprecated

hram908
  • 374
  • 6
  • 15