PHPempty and isset source code analysis was asked about php empty (0), empty (0.0), empty ('0'), empty ('0. 0 ') and empty ('00') respectively return what, for the normal empty () manual to describe themselves very clear:
"", 0, "0", NULL, FALSE, array (), var $ var; and objects without any attribute will be considered empty. If var is empty, returns TRUE.
But I was confused about empty ('00'), because in my understanding system, empty () converts '0' to a number to judge, so true is returned. will '00' be converted? Without practice, I thought for myself at the time that the returned value of '00' was false, on the grounds that '00' would not be converted into a number or a non-empty string, therefore, it is false.
Because I'm not sure, I searched google and no one studied this simple problem. I just had to dig it myself. First write a php
Opcode found:
2 0 E> ISSET_ISEMPTY_VAR 293601280 RES [IS_TMP_VAR ~ 0] OP1 [IS_CV! 0] OP2 [IS_UNUSED]
1 FREE OP1 [IS_TMP_VAR ~ 0]
3 2 ISSET_ISEMPTY_VAR 310378496 RES [IS_TMP_VAR ~ 1] OP1 [IS_CV! 0] OP2 [IS_UNUSED]
3 FREE OP1 [IS_TMP_VAR ~ 1]
4 4> RETURN OP1 [IS_CONST (128414056) 1]
ISSET_ISEMPTY_VAR is called, and ZEND_ISSET_ISEMPTY_VAR is executed by ZEND_ISSET_ISEMPTY_VAR (zend_vm_opcodes.c) in the zend source code ),
Then, find the ZEND_ISSET_ISEMPTY_VAR and execute a series of functions such as executor (zend_vm_execute.h). The final execution function to judge whether the variable is null is I _zend_is_true () by reading the source code (),
Finally, find the defined position: zend_execute.h: static zend_always_inline int I _zend_is_true (zval * op );
The code is as follows:
static zend_always_inline int i_zend_is_true(zval *op){ int result; switch (Z_TYPE_P(op)) { case IS_NULL: result = 0; break; case IS_LONG: case IS_BOOL: case IS_RESOURCE: result = (Z_LVAL_P(op)?1:0); break; case IS_DOUBLE: result = (Z_DVAL_P(op) ? 1 : 0); break; case IS_STRING: if (Z_STRLEN_P(op) == 0 || (Z_STRLEN_P(op)==1 && Z_STRVAL_P(op)[0]=='0')) { result = 0; } else { result = 1; } break; case IS_ARRAY: result = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0); break; case IS_OBJECT: if(IS_ZEND_STD_OBJECT(*op)) { TSRMLS_FETCH(); if (Z_OBJ_HT_P(op)->cast_object) { zval tmp; if (Z_OBJ_HT_P(op)->cast_object(op, &tmp, IS_BOOL TSRMLS_CC) == SUCCESS) { result = Z_LVAL(tmp); break; } } else if (Z_OBJ_HT_P(op)->get) { zval *tmp = Z_OBJ_HT_P(op)->get(op TSRMLS_CC); if(Z_TYPE_P(tmp) != IS_OBJECT) { /* for safety - avoid loop */ convert_to_boolean(tmp); result = Z_LVAL_P(tmp); zval_ptr_dtor(&tmp); break; } } } result = 1; break; default: result = 0; break; } return result;} The very simple code makes it clear that empty () does not perform any conversion on the detection value, but adds a "patch" operation when detecting the string (I understand ):
When determining a string, empty () first checks whether the length is 0. If yes, it returns 0. Otherwise, it determines whether the length is 1 and the character is '0'. If yes, it returns 0; in other cases, return 1;
As for isset (), it is returned when no value is assigned to the variable, and the I _zend_is_true () function is not executed.