PHP's global variables are a little bit different from the C language. In the C language, global variables are active in functions unless they are shrouded by local variables. This may cause some problems, and some people may inadvertently change a global variable. In PHP, global variables must be globally declared as global when applied in a function.
An example of comparison
Example one:
The code is as follows |
Copy Code |
$var 1 = 1; function Test () { unset ($GLOBALS [' var1 ']); } Test (); echo $var 1; ?> |
Because $var1 was removed, nothing was printed.
Example two:
The code is as follows |
Copy Code |
$var 1 = 1; function Test () { Global $var 1; Unset ($var 1); } Test (); echo $var 1; ?> |
Accidentally printed 1. Proof that the deletion is only an alias reference, and that its value is not changed by any of its own.
Second, explain
The global $var is actually the & $GLOBALS [' var '], calling an alias of an external variable.
The $var1 and $globals[' var1 ' in the code above refer to the same variable, not two different variables.
PHP's global variables are a little bit different from the C language. In the C language, global variables are active in functions unless they are shrouded by local variables. This may cause some problems, and some people may inadvertently change a global variable. In PHP, global variables must be globally declared as global when applied in a function.
PHP's global variable is defined as a global variable, but the global variable is not applied to the entire Web site, but is applied to the current page, including all files of include or require.
Iii. Conclusion
1. $GLOBALS [' var '] is the external global variable itself
2.global $var is a reference or pointer to an external $var with the same name.
http://www.bkjia.com/PHPjc/632199.html www.bkjia.com true http://www.bkjia.com/PHPjc/632199.html techarticle PHP's global variables are a little bit different from the C language. In the C language, global variables are active in functions unless they are shrouded by local variables. This may cause some problems, some people may not ...