你知道如下的几段代码的布尔结果分别是什么么?
var_dump("1" == "1e0"); var_dump("1" == "0x1"); var_dump("20" == "2e1"); var_dump("10" == " 0x0a");等等;
var_dump("1" == "1e0"); //true var_dump("1" == "1ef"); //false 这两个的结果为什么不一样啊.也就是说, 上面的等式a要成立, 那么唯一的解释就是PHP讲他们双发都当作int来比较.
expr T_IS_EQUAL expr { zend_do_binary_op(ZEND_IS_EQUAL, &$$, &$1, &$3 TSRMLS_CC); }那么接下就去zend_vm_def.h中寻找当ZE遇到ZEND_IS_EQUAL会做什么:
ZEND_VM_HANDLER(17, ZEND_IS_EQUAL, CONST|TMP|VAR|CV, CONST|TMP|VAR|CV) { zend_op *opline = EX(opline); zend_free_op free_op1, free_op2; is_equal_function(&EX_T(opline->result.u.var).tmp_var, GET_OP1_ZVAL_PTR(BP_VAR_R), GET_OP2_ZVAL_PTR(BP_VAR_R) TSRMLS_CC); FREE_OP1(); FREE_OP2(); ZEND_VM_NEXT_OPCODE(); }哦, 看来不是, 还要进一步查看compare_function
if (op1->type == IS_STRING && op2->type == IS_STRING) { zendi_smart_strcmp(result, op1, op2); COMPARE_RETURN_AND_FREE(SUCCESS); }
var_dump("1" == "1e0"); var_dump("1" == "0x1"); var_dump("20" == "2e1"); var_dump("10" == " 0x0a");呵呵, 读者们遇到过这个问题的困扰么?
if("608E-4234" == "272E-3063"){ echo "oh shit! they're equal!\n"; }else{ echo "obviously, isn't?\n"; }
PHP开发Tips:如果想要判断两个字符串是否相等,使用 === 而不是 ==。
作者: Laruence
本文地址: http://www.laruence.com/2009/02/21/662.html