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?

5
  • (related) stackoverflow.com/questions/2524680/… Commented Apr 1, 2010 at 11:23
  • 3
    what should it return for "+3928742938749283479823749283749" ? Commented Apr 1, 2010 at 11:24
  • @stereofrog: textbox length validation may block you for this value in my application :) Commented Apr 1, 2010 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 Commented Apr 1, 2010 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 :) Commented Apr 1, 2010 at 11:53

6 Answers 6

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 )
Sign up to request clarification or add additional context in comments.

4 Comments

@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.
(int)$num == $num (int)'abcde' == 'abcde' will return true
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...
I solved it this way: $num = (int)$num >= 0 ? (int)$num : 0;
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)

7 Comments

and what about abcd, 123d, 10.5 and (space) ??
@NAVEED try yourself and tell me :) I predict all false
You have to wait.. leaving ;)
+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.
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"); }
|
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.

Comments

3

I would say this is the best way

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

as typecasting to an int is very slow.

2 Comments

But get in mind with this answer that if $num is an integer string as $num = '123', is_int($num) return false
I use is_numeric() to check if the given string is a valid number
1

the easiest way is:

if intval($x) > 0 {
 echo "true"
}

1 Comment

intval('1e10'); // 1 and intval('420000000000000000000'); // 2147483647
0

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

Deprecated: Function ereg() is deprecated

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.