1

Possible Duplicate:
PHP: Shortest way to check if a variable contains positive integer?

How do I know, is a given variable a number without residue (and not a negative number)?

Like we have numbers (each number is a variable): 5; 6.416; -76; 254.

Its better to have some function, which would give true or false, like:

number "5" is true
6.416 is false (has residue "416")
-76 is false
254 is true.

Thanks.

Community
  • 1
  • 1
James
  • 42,081
  • 53
  • 136
  • 161

2 Answers2

3
if ( (int)$num == $num && (int)$num > 0 ){
  return true;
}
Naveed
  • 41,517
  • 32
  • 98
  • 131
2

This should do it:

function is_positive_int($n) {
    return $n && abs(intval($n)) == $n;
}
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356