【问题标题】:Question about integers (php) [duplicate]关于整数的问题(php)[重复]
【发布时间】:2011-06-15 01:12:49
【问题描述】:

可能重复:
PHP: Shortest way to check if a variable contains positive integer?

我怎么知道,给定的变量是一个没有余数的数字(而不是一个负数)?

就像我们有数字一样(每个数字都是一个变量):5; 6.416; -76; 254.

最好有一些函数,它会给出真假,比如:

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

谢谢。

【问题讨论】:

    标签: php integer


    【解决方案1】:

    应该这样做:

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

    【讨论】:

      【解决方案2】:
      if ( (int)$num == $num && (int)$num > 0 ){
        return true;
      }
      

      【讨论】:

        猜你喜欢
        • 2011-09-03
        • 2011-11-14
        • 2011-09-22
        • 2015-01-03
        • 1970-01-01
        • 2021-03-29
        • 1970-01-01
        • 2013-03-05
        • 1970-01-01
        相关资源
        最近更新 更多