Home >Backend Development >PHP Tutorial >The difference between PHP global variables global and $GLOBALS[]
Since $var1 is deleted, nothing is printed. Example 2:
Accidentally printed 1. It proves that only the alias reference is deleted, and its value is not changed in any way. 2. Explanation global $var is actually &$GLOBALS['var'], which is just an alias for calling external variables. $var1 and $GLOBALS['var1'] in the above code refer to the same variable, not two different variables. PHP's global variables are a little different from C language. In C language, global variables automatically take effect in functions unless covered by local variables. This can cause problems, as someone might carelessly mutate a global variable. Global variables in PHP must be declared as global using global when used in functions. The function of PHP's Global variable is to define global variables, but this global variable does not apply to the entire website, but to the current page, including all files in include or require. 3. Summary 1.$GLOBALS['var'] is the external global variable itself 2.global $var is a reference or pointer of the same name as an external $var. |