The use and difference between global and $ GLOBALS [] variables in PHP is the context defined by the variable (that is, its effective range ). Most PHP variables have only one separate range. This separate range span also contains the files introduced by include and require. Example :? & Lt ;? Php $ a1; includeb. inc ;? & Gt ;?? Here, the variable $ a will be used and used in the inclusion file PHP for global and $ GLOBALS [].
The scope of a variable is the context defined by it (that is, its effective range ). Most PHP variables have only one separate range. This separate range span also contains the files introduced by include and require. For example:
?
?
?
The variable $ a will take effect in the included file B. inc. However, in user-defined functions, a local function range will be introduced. By default, any variable used in a function is limited to a local function. For example:
?
?
?
This script does not have any output, because the echo statement references a local version of variable $ a and is not assigned a value within this range. You may notice that the global variables in PHP are a little different from those in C language. in C language, global variables automatically take effect in functions unless they are overwritten by local variables. This may cause some problems. some may accidentally change a global variable. Global variables in PHP must be declared as global when used in functions.
?
Global keyword
?
First, an example of using global:
Example #1 use global
?
?
?
The output of the above script is "3 ". After the global variables $ a and $ B are declared in the functionReferenceIt will point to its global version. PHP has no limit on the maximum number of global variables that a function can declare.
The second way to access variables globally is to use a special PHP custom $ GLOBALS array. The preceding example can be written as follows:
Example #2 replace global with $ GLOBALS
?
?
?
$ GLOBALS is an associated array. each variable is an element. The key name corresponds to the variable name and the value corresponds to the variable content. $ GLOBALS exists globally because $ GLOBALS isSuper global variable. The following example shows how to use a super global variable:
Example #3 examples of hyperglobal variables and scopes
?
?
?
?Difference between global and $ GLOBALS []
?
$ GLOBALS ['var'] is an external global variable.Itself, Global $ var is external $ varReference or pointer with the same name
?
?Result:Notice: Undefined variable: var1. The variable var1 is deleted by the unset function.
?
? Result: Print 1.
Only alias reference is deleted.And its own value is not changed.
Global $ var is actually& $ GLOBALS ['var'], Call an alias of an external variable .?
$ Var1 and $ GLOBALS ['var1'] in the above code refer to the same variable itself.
?
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 of include or require .?
?
Conclusion:
1. $ GLOBALS ['var'] is an external global variable itself?
2. global $ var is an external reference or pointer of $ var with the same name..
?
?