有个不太明白的地方

2013-06-11 22:21:09 +08:00
 kennedy32
<?php

$a="1000"; $b="+1000";
if($a == $b) echo "1";
if($a !== $b) echo "2";

?>

最后输出12
我不太懂,为什么$a==$b和$a!==$b都true呢?
3186 次点击
所在节点    PHP
9 条回复
qiayue
2013-06-11 22:35:53 +08:00
第一句你用三个等号试一试?
kennedy32
2013-06-11 22:38:55 +08:00
@qiayue 我知道如果===就是false,但是不太明白两个怎么可能又==又!==

所以!==是not ===的意思?
gDD
2013-06-11 22:43:37 +08:00
@kennedy32 是的,!=才是not ==的意思。
scusjs
2013-06-11 22:44:10 +08:00
@kennedy32 !先!再==,所以就是!(==),表示a和b不相等
Sunyanzi
2013-06-12 17:05:15 +08:00
RTFM ...

--------------------

If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.

<?php
var_dump(0 == "a"); // 0 == 0 -> true
var_dump("1" == "01"); // 1 == 1 -> true
var_dump("10" == "1e1"); // 10 == 10 -> true
var_dump(100 == "1e2"); // 100 == 100 -> true

switch ("a") {
case 0:
echo "0";
break;
case "a": // never reached because "a" is already matched with 0
echo "a";
break;
}
msg7086
2013-06-12 17:30:17 +08:00
var_dump("1" == "01"); // 1 == 1 -> true
var_dump("10" == "1e1"); // 10 == 10 -> true

对这两句话感到绝望了……
kennedy32
2013-06-12 20:09:33 +08:00
@gDD 感谢3楼,正解。

其他楼的人要么把问题简单化,要么把问题复杂化
F0ur
2013-06-18 01:21:46 +08:00
==是等于 !=是不等于
===是类型和数值都相等,曰恒等
!==是类型或数值不相等
jevonszmx
2013-07-02 21:55:48 +08:00
1、==或者!=,手册:如果比较一个整数和字符串,则字符串会被转换为整数。
2、!==是===的相反,会同时比较值和类型的。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/72077

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX