php 运算符 == (某种相等)和整数 0
php,我最亲爱的老朋友。
好的,所以我可以理解为什么字符串“0”是一个假值。这是公平的,因为 '0'
与松散类型语言中的 0
相同,而 0
是 false 采用松散类型语言。所以这给出了
false
== 0
== '0'
。
好吧好吧……但是!!这是怎么回事?
<?php
print "number of surprised persons: " . ('false' == 0);
输出是......
number of surprised persons: 1
这怎么合理?我是唯一一个对此感到惊讶的人吗?我没看到什么?
进一步的测试证明整数 0 是相等的(通过运算符 ==),
0 <-- integer
false <-- boolean
null <-- just.. you know, null
'0' <-- string
'' <-- string
'false' <-- string
'true' <-- string
'null' <-- string
自然地,我主要使用运算符 === 来比较东西。既然我知道了这一点,我当然必须调整我的编程,这是毫无疑问的。但仍然!有人可以解释一下pl0x吗?
php, my dearest old frienemy.
ok, so i can come to terms with why the string '0' would be a falsie value. that's only fair seeing as how '0'
is the same as 0
in a loosely typed language, and 0
is false
in a loosely typed language. so this gives that false
== 0
== '0'
.
fine fine... BUT!! what is this all about?
<?php
print "number of surprised persons: " . ('false' == 0);
the output is....
number of surprised persons: 1
how is this reasonable? am i the only one who's surprised by this? what am i failing to see?
further testing has proven that the integer 0 is equal (by operator ==) to
0 <-- integer
false <-- boolean
null <-- just.. you know, null
'0' <-- string
'' <-- string
'false' <-- string
'true' <-- string
'null' <-- string
naturally, i mostly use operator === to compare stuff. and now that i know about this, i'll have to adjust my programming of course, no question about that. but still! can someone shed some light pl0x?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为,当您将字符串与整数进行比较时,它们不会同时转换为字符串或布尔值 - 它们会转换为整数。对于 PHP,当你仔细想想,这一点也不奇怪(相对而言,我想)。
对于任何非数字字符串以及字符串
"0"
都是如此。之所以打印出1
,是因为true
的字符串版本是1
(而false的字符串版本是空字符串)。It's because, when you compare a string to an integer, they don't both get converted to strings, or to booleans - they get converted to integers. For PHP, when you think about it, this isn't strange at all (comparatively, I suppose).
And this is true for any non-numeric string as well as the string
"0"
. The reason1
is printed out is because the string version oftrue
is1
(and the string version of false is an empty string).就您关心的输出而言:
('false' == 0)
= booleanTRUE
= string"1"
。echo
正在触发字符串上下文。但从你下面的评论中我刚刚看到,你想了解更多有关比较的信息。看看你做了什么:
你正在对数字与字符串进行非严格比较:
注意:数字,而不是接受答案中写入的整数:
玩得开心。
As far as you're concerned about the output:
('false' == 0)
= booleanTRUE
= string"1"
.echo
is triggering string context.But from your comment below I've just seen, that you'd like to learn more about the comparison. Take a look what you do:
so you are doing a non-strict comparison of a number with a string:
Note: Number, not integer as written in the accepted answer:
Have fun.
假 == 0 为真。 True 作为字符串为“1”,并且在插入值时正在进行隐式转换。
您可以在此处找到 PHP 真值表。我只是推荐 === 比较器,除非你有充分的理由使用 ==
http: //php.net/manual/en/types.comparisons.php
false == 0 is true. True as a string is '1' and you are doing an implicit conversion when you interpolate the value.
You can find a PHP truth table here. I would just recommend the === comparator unless you have a good reason to use ==
http://php.net/manual/en/types.comparisons.php