V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
kennedy32
V2EX  ›  PHP

有个不太明白的地方

  •  
  •   kennedy32 · Jun 11, 2013 · 3780 views
    This topic created in 4708 days ago, the information mentioned may be changed or developed.
    <?php

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

    ?>

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

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

    对这两句话感到绝望了……
    kennedy32
        7
    kennedy32  
    OP
       Jun 12, 2013
    @gDD 感谢3楼,正解。

    其他楼的人要么把问题简单化,要么把问题复杂化
    F0ur
        8
    F0ur  
       Jun 18, 2013
    ==是等于 !=是不等于
    ===是类型和数值都相等,曰恒等
    !==是类型或数值不相等
    jevonszmx
        9
    jevonszmx  
       Jul 2, 2013
    1、==或者!=,手册:如果比较一个整数和字符串,则字符串会被转换为整数。
    2、!==是===的相反,会同时比较值和类型的。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2346 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 97ms · UTC 04:40 · PVG 12:40 · LAX 21:40 · JFK 00:40
    ♥ Do have faith in what you're doing.