Global variables in PHP must be declared as Global when used in functions (the Global keyword is defined in the function ). Global variables in PHP must be declared as Global when used in functions (the Global keyword is defined in the function ).
Global is used to define Global variables. However, this Global variable is not applied to the entire website, but to the current page, including all files including include or require.
Global variables defined in the function body can be used in vitro. global variables defined in the function body cannot be used in the function body.
$ Global $ a; $ a = 123; function f () {echo $ a; // error,} // check the following function f () {global $; $ a = 123;} f (); echo $ a; // correct, available
Example comparison:
Because $ var1 is deleted, nothing is printed.
Accidentally printed 1. It indicates that only alias reference is deleted, and its value is not changed.
Global $ var is actually an alias for calling an external variable & $ GLOBALS ['var.
$ Var1 and $ GLOBALS ['var1'] in the above code refer to the same variable, rather than two different variables.
The global variables of PHP are a little different from those of C. In C, global variables take effect in functions unless they are shrouded in local variables. This may cause some problems. some may carelessly change a global variable. Global variables in PHP must be declared as global in the function.
PHP's Global variable defines Global variables, but this Global variable is not applied to the entire website, but to the current page, including all files including include or require.
$ GLOBALS reference to the php manual:
Global variable: $ GLOBALS. note: $ GLOBALS is applicable to PHP 3.0.0 and later versions.
An array composed of all defined global variables. The variable name is the index of the array. This is a "superglobal", or it can be described as an automatic global variable.
That is to say, $ var1 and $ GLOBALS ['var1'] in the above code refer to the same variable, rather than two different variables!
If a global variable is assigned to a reference within a function, the reference is only visible within the function. You can avoid this by using the $ GLOBALS array.
We all know that all the variables produced by functions in php are private variables of the function, so the variables generated by the global keyword certainly cannot escape this rule, global generates an alias variable pointing to the external variable of the function, instead of the real external variable of the function. Once the pointing address of the alias variable is changed, unexpected situations may occur, $ GLOBALS [] is actually called as an external variable, and the internal and external functions will always be consistent.
The output will be "3 ″. The global variables $ a and $ B are declared in the function. all referenced variables of any variable point to the global variable.
Why isn't it 2? isn't it not affected outside the function? please note that $ B does not reference and modify in the function, but the modified $ B points to the physical memory value, therefore, the external input is 3.
So we come to the conclusion that the difference between global and $ GLOBALS [] in the function is:
Global generates an alias variable pointing to the external variable of the function, instead of the real external variable of the function. Once the pointing address of the alias variable is changed, unexpected situations may occur.
$ GLOBALS [] is actually called as an external variable, and the internal and external functions will always be consistent!