What is the difference between <> and !=

This question already has an answer here:

  • Difference between “not equal” operators <> and != in PHP 6 answers

  • Forgetting documentation for a minute, let's check out the source code. Let's start with the scanner (lexer):

    <ST_IN_SCRIPTING>"!="|"<>" {
        return T_IS_NOT_EQUAL;
    }
    

    So they parse to the same token. Let's check out the parser:

    expr T_IS_NOT_EQUAL expr { zend_do_binary_op(ZEND_IS_NOT_EQUAL, &$$, &$1, &$3 TSRMLS_CC); }
    

    So we know that the opcode that's fired is ZEND_IS_NOT_EQUAL ...

    Now, let's check out the operation:

    static int ZEND_FASTCALL  ZEND_IS_NOT_EQUAL_SPEC_CONST_CONST_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
    {
        USE_OPLINE
    
        zval *result = &EX_T(opline->result.var).tmp_var;
    
        SAVE_OPLINE();
        ZVAL_BOOL(result, fast_not_equal_function(result,
            opline->op1.zv,
            opline->op2.zv TSRMLS_CC));
    
        CHECK_EXCEPTION();
        ZEND_VM_NEXT_OPCODE();
    }
    

    So there's literally no difference. Since they parse to the same token, they have exactly the same precedence (so the docs are either wrong or misleading). Since they use the same executor, and there's no decision point in the opcode routine, they execute identical code.

    So yes, <> and != are 100% interchangeable, and there's absolutely no technical reason to use one over the other.

    With that said, there is something significant to gain by being consistent. So I'd recommend just sticking with != and being done with it...

    Edit

    I've updated the docs to reflect this, and fixed another issue with the precedence order (++ and -- have the same precedence as casting). Check it out on docs.php.net


    No difference.

    However, != allows the convenience of more easily adding an extra = to force type comparison.


    One's old, one's new.

    according to the manual:

    $a != $b    Not equal   TRUE if $a is not equal to $b after type juggling.
    $a <> $b    Not equal   TRUE if $a is not equal to $b after type juggling.
    

    use != .

    The minor difference: there is also an order of precedence. look here.

    <> comes before != in the precedence table, but they accomplish the exact same thing.

    链接地址: http://www.djcxy.com/p/1806.html

    上一篇: PHP运算符<>

    下一篇: <>和!=之间有什么区别?