Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortest way to check if a variable contains positive integer using PHP?

Tags:

types

php

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?

like image 267
Naveed Avatar asked Apr 01 '10 11:04

Naveed


People also ask

How do you check if a variable is an integer in PHP?

The is_int() function checks whether a variable is of type integer or not. This function returns true (1) if the variable is of type integer, otherwise it returns false.

How do you check if a number is a positive integer?

If the Integer is greater than zero then it is a positive integer. If the number is less than zero then it is a negative integer. If the number is equal to zero then it is neither negative nor positive.


5 Answers

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 )
like image 175
Yacoby Avatar answered Sep 29 '22 21:09

Yacoby


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)

like image 39
Gordon Avatar answered Sep 29 '22 20:09

Gordon


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.

like image 38
Imabigfatseacow Avatar answered Sep 29 '22 20:09

Imabigfatseacow


I would say this is the best way

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

as typecasting to an int is very slow.

like image 29
Decko Avatar answered Sep 29 '22 20:09

Decko


the easiest way is:

if intval($x) > 0 {
 echo "true"
}
like image 43
Nopcea Flavius Avatar answered Sep 29 '22 20:09

Nopcea Flavius