为什么 (0 == 'Hello') 在 PHP 中返回 true?
嘿,如果您有以下代码并想检查 $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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
运算符
==
和!=
不比较类型。因此,PHP 会自动将“Hello”转换为整数0
(intval('Hello')
)。当不确定类型时,请使用类型比较运算符===
和!==
。或者更好地确定您在程序中的任何点处理哪种类型。The operators
==
and!=
do not compare the type. Therefore PHP automatically converts 'Hello' to an integer which is0
(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.其他人已经很好地回答了这个问题。我只想举一些其他的例子,你应该知道,所有这些都是由 PHP 的类型杂乱引起的。以下所有比较都将返回 true:
因为我发现这种行为很危险,所以我编写了自己的 equals方法并在我的项目中使用它:
希望这可以帮助某人使他的应用程序更加可靠,原始文章可以在这里找到:
等于或不等于
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:
Because i found this behaviour dangerous, i wrote my own equal method and use it in my projects:
Hope this helps somebody making his application more solid, the original article can be found here:
Equal or not equal
几乎任何非零值都会在 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
因为 PHP 会自动进行转换来比较不同类型的值。您可以在 PHP 文档中查看类型转换标准表。
在您的例子中,字符串
"Hello"
会自动转换为数字,根据 PHP,该数字为0
。因此真正的价值。如果您想比较不同类型的值,您应该使用类型安全运算符:
或
一般来说,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 is0
according to PHP. Hence the true value.If you want to compare values of different types you should use the type-safe operators:
or
In general, PHP evaluates to zero every string that cannot be recognized as a number.
在 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