【问题标题】:PHP compare doubtPHP比较疑惑
【发布时间】:2010-06-03 10:02:22
【问题描述】:
if(0 == ('Pictures'))
{
  echo 'true';
}

为什么它给了我“真实”?

【问题讨论】:

标签: php operators equality comparison-operators


【解决方案1】:

您的字符串将被评估为整数,因此变为 0,使用此:0 === 'Pictures' 验证身份(相同的值和相同的类型)

【讨论】:

    【解决方案2】:

    查看PHP type comparison tables 以了解比较运算符在 PHP 中的行为方式。

    在您的情况下,“图片”变为“0”,因此 0 = 0。

    让我们看看下面的例子:

    echo (int)'Pictures'; // 0 => 'Picture' as int
    echo 0 == 'Pictures'; // 1 => true, 0 = 0
    

    【讨论】:

      【解决方案3】:

      用途:

      if (0 === 'Pictures')
      {
        echo 'true';
      }
      

      === 是严格的类型运算符,它不仅检查值,还检查类型。

      快速测试:

      if(0 == 'Pictures')
      {
        echo 'true';
      }
      else
      {
        echo 'false';
      }
      

      输出true 但是:

      if(0 === 'Pictures')
      {
        echo 'true';
      }
      else
      {
        echo 'false';
      }
      

      输出false

      【讨论】:

        猜你喜欢
        • 2015-08-20
        • 2020-11-02
        • 2017-02-16
        • 2013-09-06
        • 1970-01-01
        • 1970-01-01
        • 2013-12-10
        • 1970-01-01
        • 2013-04-16
        相关资源
        最近更新 更多