V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
kennedy32
V2EX  ›  PHP

有个不太明白的地方

  •  
  •   kennedy32 · 2013-06-11 22:21:09 +08:00 · 3167 次点击
    这是一个创建于 3943 天前的主题,其中的信息可能已经有所发展或是发生改变。
    <?php

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

    ?>

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

    所以!==是not ===的意思?
    gDD
        3
    gDD  
       2013-06-11 22:43:37 +08:00   ❤️ 1
    @kennedy32 是的,!=才是not ==的意思。
    scusjs
        4
    scusjs  
       2013-06-11 22:44:10 +08:00
    @kennedy32 !先!再==,所以就是!(==),表示a和b不相等
    Sunyanzi
        5
    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
        6
    msg7086  
       2013-06-12 17:30:17 +08:00
    var_dump("1" == "01"); // 1 == 1 -> true
    var_dump("10" == "1e1"); // 10 == 10 -> true

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

    其他楼的人要么把问题简单化,要么把问题复杂化
    F0ur
        8
    F0ur  
       2013-06-18 01:21:46 +08:00
    ==是等于 !=是不等于
    ===是类型和数值都相等,曰恒等
    !==是类型或数值不相等
    jevonszmx
        9
    jevonszmx  
       2013-07-02 21:55:48 +08:00
    1、==或者!=,手册:如果比较一个整数和字符串,则字符串会被转换为整数。
    2、!==是===的相反,会同时比较值和类型的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3202 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 14:25 · PVG 22:25 · LAX 07:25 · JFK 10:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.