Home  >  Q&A  >  body text

关于php 位运算 移位的疑惑

var_dump( (pack("C" , 1)<<1) ==pack("C" , 1) );
为什么是true呢

PHP中文网PHP中文网2713 days ago427

reply all(1)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-16 13:11:55

    When it comes to PHP comparison, type conversion
    1, pack("C", 1)<<1, the result is int(0)
    2, pack("C", 1), the result is string(" ")

    In fact, it is simplified to the comparison of 0 == ""

    PHP 在比较的时候,如果类型不相等,会进行类型转换
    Here string("") will be converted to int type, that is, it will become int(0), so it returns true

    You have to judge strictly, you can use ===

    var_dump( (pack("C" , 1)<<1) ===pack("C" , 1) ); The result is false

    reply
    0
  • Cancelreply