For example:
Copy CodeThe code is as follows:
$a = 1;
Include ' B.inc ';
?>
Here the variable $a will take effect in the include file B.inc. However, in a user-defined function, a local function scope is introduced. Any variables used inside the function will be limited to the local function by default, and this is a local variable.
In PHP, global variables must be declared as globals when used in functions.
A variable declared with global in a function is a global variable that can be used outside of a function. Note: When global declares a variable, it cannot directly assign a value to the variable, it needs to be declared and then assigned.
In the global scope, you can also
$GLOBALSAccessing global variables, no need to use them within functions
GlobalKeyword to access the global variable. $GLOBALS is an associative array, each variable is an element, the key name corresponds to the variable name, and the value corresponds to the contents of the variable. $GLOBALS exists globally because $GLOBALS is a hyper-global variable.
Constants can be defined and accessed anywhere, regardless of the scope of the variable;
Another important feature of the variable range is the static variable (
static variable)。 A static variable exists only in the local function domain, but its value is not lost when the program executes away from the scope. A static variable is initialized only on the first call and can be assigned a value when declared, and cannot be an expression value. If you assign a value to a declaration with the result of an expression, it causes a parse error.
When assigning a reference (a variable or object with &) to a static variable, the reference is not stored statically, and the value of the static variable is not remembered when the function is called the second time. Similarly, when a reference (a variable or object with &) is assigned to the global variable, the change in the variable does not work outside the function, and the scope is only within that function.
http://www.bkjia.com/PHPjc/326212.html www.bkjia.com true http://www.bkjia.com/PHPjc/326212.html techarticle For example: the copy code code is as follows: PHP $a = 1; include ' b.inc '; Here the variable $a will take effect in the include file B.inc. However, in a user-defined function, a local function fan ...