开发者

Question about integers (php) [duplicate]

This question already has answers here: Closed 12 years ago.

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开发者_如何学Go false, like:

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

Thanks.


if ( (int)$num == $num && (int)$num > 0 ){
  return true;
}


This should do it:

function is_positive_int($n) {
    return $n && abs(intval($n)) == $n;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜