PHP source code reading: the empty and isset functions have recently been asked how to judge the variables when the empty and isset functions are in PHP. at first, I was overwhelmed, because I was just a bit confused, in order to understand its true principles, we should quickly open the source code Research. After analysis, we can find that both functions call the same function. Therefore, this article will analyze the two functions together.
Function format: empty
bool empty ( mixed $var )
Determines whether the variable is null.
Isset
bool isset ( mixed $var [ , mixed $... ] )
Determines whether the variable is set and not NULL.
Parameter description
For empty, before PHP5.5, empty only supports variable parameters. other types of parameters may cause parsing errors. for example, function call results cannot be used as parameters.
For isset, if the variable is set as NULL by the unset function, the function returns false. If multiple parameters are passed to the isset function, true is returned only when all parameters are set to the isset function. Calculated from left to right. Once a variable is not set, it is stopped.
Running example
$ Result = empty (0); // true $ result = empty (null); // true $ result = empty (false ); // true $ result = empty (array (); // true $ result = empty ('0'); // true $ result = empty (1 ); // false $ result = empty (callback function); // error $ a = null; $ result = isset ($ a); // false; $ a = 1; $ result = isset ($ a); // true; $ a = 1; $ B = 2; $ c = 3; $ result = isset ($ a, $ B, $ c); // true $ a = 1; $ B = null; $ c = 3; $ result = isset ($ a, $ B, $ c); // false
Locate the function definition location
In fact, empty is not a function, but a language structure. The language structure is compiled before the PHP program runs, so it cannot be simply searched as beforePHP_FUNCTION emptyOrZEND_FUNCTION emptyView its source code. To view the source code of empty and other language structures, you must first understand the PHP code execution mechanism.
PHP executes the code in four steps. the flowchart is as follows:
In the first phase, that is, the Scanning phase, the program will scanZend_language_scanner.lFile to convert the code file into a language segment. For isset and empty functionsZend_language_scanner.lSearch for empty and isset in the file. the macro definition of the function in this file is as follows:
"isset" {return T_ISSET;}
"empty" {return T_EMPTY;}
Next we will go to the Parsing stage. at this stage, the program converts Tokens such as T_ISSET and T_EMPTY into meaningful expressions, and then performs syntax analysis. the Tokens yacc is saved in the zend_language_parser.y file, you can find the definitions of T_ISSET and T_EMPTY:
Internal_functions_in_yacc: T_ISSET '('isset_variables') '{\( = $3 ;}| T_EMPTY' ('variable') '{substring (ZEND_ISEMPTY ,&\)