【问题标题】:Why does (0 == 'Hello') return true in PHP?为什么 (0 == 'Hello') 在 PHP 中返回 true?
【发布时间】:2011-05-05 07:49:06
【问题描述】:

嘿,如果您有以下代码并想检查$key 是否与Hello 匹配,我发现,如果变量为0,比较总是返回true。当一个特殊键的数组时,我遇到了这个问题,并想知道为什么它没有按预期工作。 有关示例,请参见此代码。

$key = 1;
if ($key != 'Hello') echo 'Hello'; //echoes hello

$key = 2;
if ($key != 'Hello') echo 'Hello'; //echoes hello

$key = 0;
if ($key != 'Hello') echo '0Hello'; //doesnt echo hello. why?
if ($key !== 'Hello') echo 'Hello'; //echoes hello

谁能解释一下?

【问题讨论】:

  • 有趣的是,有多少人没有正确阅读或理解这个问题。

标签: php if-statement


【解决方案1】:

运算符==!= 不比较类型。因此 PHP 会自动将 'Hello' 转换为整数,即 0 (intval('Hello'))。如果不确定类型,请使用类型比较运算符 ===!==。或者更好地确定您在程序中的任何时候处理的类型。

【讨论】:

  • +1 用于了解您正在处理的类型。不幸的是,API 或文档中经常忽略类型提示。
【解决方案2】:

其他人已经很好地回答了这个问题。我只是想举一些其他的例子,你应该知道,都是由PHP的类型杂耍引起的。以下所有比较都将返回 true

  • 'abc' == 0
  • 0 == 空
  • '' == null
  • 1 == '1y?z'

因为我发现这种行为很危险,所以我编写了自己的 equal 方法并在我的项目中使用它:

/**
 * Checks if two values are equal. In contrast to the == operator,
 * the values are considered different, if:
 * - one value is null and the other not, or
 * - one value is an empty string and the other not
 * This helps avoid strange behavier with PHP's type juggling,
 * all these expressions would return true:
 * 'abc' == 0; 0 == null; '' == null; 1 == '1y?z';
 * @param mixed $value1
 * @param mixed $value2
 * @return boolean True if values are equal, otherwise false.
 */
function sto_equals($value1, $value2)
{
  // identical in value and type
  if ($value1 === $value2)
    $result = true;
  // one is null, the other not
  else if (is_null($value1) || is_null($value2))
    $result = false;
  // one is an empty string, the other not
  else if (($value1 === '') || ($value2 === ''))
    $result = false;
  // identical in value and different in type
  else
  {
    $result = ($value1 == $value2);
    // test for wrong implicit string conversion, when comparing a
    // string with a numeric type. only accept valid numeric strings.
    if ($result)
    {
      $isNumericType1 = is_int($value1) || is_float($value1);
      $isNumericType2 = is_int($value2) || is_float($value2);
      $isStringType1 = is_string($value1);
      $isStringType2 = is_string($value2);
      if ($isNumericType1 && $isStringType2)
        $result = is_numeric($value2);
      else if ($isNumericType2 && $isStringType1)
        $result = is_numeric($value1);
    }
  }
  return $result;
}

希望这有助于某人使他的应用程序更加可靠,原始文章可以在这里找到: Equal or not equal

【讨论】:

  • 不看代码,你的函数对===操作符有什么好处?
  • 这和你有同样的优势,使用 == 操作符,你可以比较两个变量,即使它们有不同的类型。 PHP 不能很好地支持您显式控制类型,并且函数通常不知道传入的参数是数字8 还是用户输入'8'。然后,当您无论如何都可以比较它们时,您会很高兴。否则,您将不得不在函数本身中为所有可能的类型编写代码。
【解决方案3】:

几乎所有非零值都会在 php 后台转换为 true。

所以 1、2、3、4、'Hello'、'world' 等都等于 true,而 0 等于 false

!== 起作用的唯一原因是因为它比较的数据类型也相同

【讨论】:

    【解决方案4】:

    因为 PHP 会自动转换来比较不同类型的值。您可以在 PHP 文档中看到 table of type-conversion criteria

    在您的情况下,字符串"Hello" 会自动转换为一个数字,根据PHP 是0。因此才是真正的价值。

    如果你想比较不同类型的值,你应该使用类型安全的操作符:

    $value1 === $value2;
    

    $value1 !== $value2;
    

    一般来说,PHP 将每个无法识别为数字的字符串计算为零。

    【讨论】:

      【解决方案5】:

      在php中,字符串“0”被转换为布尔值FALSE http://php.net/manual/en/language.types.boolean.php

      【讨论】:

        猜你喜欢
        • 2014-09-25
        • 2015-06-21
        • 2011-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-03
        • 1970-01-01
        • 2019-12-07
        相关资源
        最近更新 更多