【问题标题】:Whats the difference between evaluating a string vs an integer in PHP?在 PHP 中评估字符串与整数有什么区别?
【发布时间】:2013-07-14 19:14:16
【问题描述】:

我最近遇到了一个奇怪的问题。

 if($somestring != "" && $somestring > 0){
  // do something.
 }

我的问题是,这两个检查是否相同?在 PHP 中转换为 INT 时字符串是否总是大于 0?或者一个字符串可以计算为 0?

【问题讨论】:

    标签: php string int


    【解决方案1】:

    可以有字符串“-1”,直接计算为-1 int

    关于您的情况 - 首先检查它是否不为空 - 在这种情况下,它将被评估为 0。看起来作者不希望 php 这样做并需要该值

    【讨论】:

      【解决方案2】:

      也许这就是你要找的……?

      http://us3.php.net/types.comparisons

      • "php" == 0 是真的。
      • "php" === 0 是假的。

      【讨论】:

        【解决方案3】:

        首先字符串可以为“空”,因此其中没有任何内容。 未设置时为整数,默认设置为 0

        所以分解是

        if ($somestring != "") // 1 - check to see if the string is not empty
        if ($somestring > 0)   // 2 - Check to see of the non-empty string has a value greater than 0
        

        所以作者试图防止松散类型的脚本。

        希望这会有所帮助:)

        【讨论】:

          猜你喜欢
          • 2011-01-15
          • 2016-12-02
          • 2012-03-22
          • 1970-01-01
          • 2011-03-03
          • 2013-02-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-29
          相关资源
          最近更新 更多