为什么 (0 == 'Hello') 在 PHP 中返回 true?

发布于 2024-11-04 21:13:39 字数 449 浏览 7 评论 0原文

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

$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

谁能解释一下吗?

Hey, if you have got the following code and want to check if $key matches Hello I've found out, that the comparison always returns true if the variable is 0. I've came across this when an array for a special key and wondered why it's wasn't working as expected.
See this code for an example.

$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

Can anyone explain this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

醉梦枕江山 2024-11-11 21:13:39

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

The operators == and != do not compare the type. Therefore PHP automatically converts 'Hello' to an integer which is 0 (intval('Hello')). When not sure about the type, use the type-comparing operators === and !==. Or better be sure which type you handle at any point in your program.

油饼 2024-11-11 21:13:39

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

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

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

/**
 * 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;
}

希望这可以帮助某人使他的应用程序更加可靠,原始文章可以在这里找到:
等于或不等于

Others have already answered the question well. I only want to give some other examples, you should be aware of, all are caused by PHP's type juggling. All the following comparisons will return true:

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

Because i found this behaviour dangerous, i wrote my own equal method and use it in my projects:

/**
 * 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;
}

Hope this helps somebody making his application more solid, the original article can be found here:
Equal or not equal

七七 2024-11-11 21:13:39

几乎任何非零值都会在 php 中在后台转换为 true。

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

!== 有效的唯一原因是因为它比较的数据类型也相同

pretty much any non-zero value gets converted to true in php behind the scenes.

so 1, 2,3,4, 'Hello', 'world', etc would all be equal to true, whereas 0 is equal to false

the only reason !== works is cause it is comparing data types are the same too

浴红衣 2024-11-11 21:13:39

因为 PHP 会自动进行转换来比较不同类型的值。您可以在 PHP 文档中查看类型转换标准表

在您的例子中,字符串 "Hello" 会自动转换为数字,根据 PHP,该数字为 0。因此真正的价值。

如果您想比较不同类型的值,您应该使用类型安全运算符:

$value1 === $value2;

$value1 !== $value2;

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

Because PHP does an automatic cast to compare values of different types. You can see a table of type-conversion criteria in PHP documentation.

In your case, the string "Hello" is automatically converted to a number, which is 0 according to PHP. Hence the true value.

If you want to compare values of different types you should use the type-safe operators:

$value1 === $value2;

or

$value1 !== $value2;

In general, PHP evaluates to zero every string that cannot be recognized as a number.

满身野味 2024-11-11 21:13:39

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

In php, the string "0" is converted to the boolean FALSE http://php.net/manual/en/language.types.boolean.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文