For example:
CopyCodeThe Code is as follows: <? PHP
$ A = 1;
Include 'B. inc ';
?>
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.
Global variables in PHP must be declared as global when used in functions.
The global declared variable in a function can be used outside the function. Note: When global declares a variable, you cannot assign a value directly to the variable. You must declare the value first and then assign a value.
You can also use$ GlobalsAccessing global variables is not required in the functionGlobalKeyword to access global variables. $ 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 is a super global variable.
Constants can be defined and accessed anywhere regardless of the scope of variables;
Another important feature of variable range is static variables (Static variable). Static variables only exist in local function domains.ProgramWhen Execution leaves this scope, its value is not lost. Static variables are initialized only when they are called for the first time. You can assign values to them when declaring them. They cannot be expression values. If a value is assigned to the expression result in the declaration, the parsing error occurs.
When a reference (a variable or object with &) is assigned to a static variable, the reference is not stored statically, the value of the static variable is not remembered when the second function is called. Similarly, when a reference (with & variable or object) is assigned to a global variable, the change of this variable does not work outside the function, and the scope of this variable is only within the function.