- $ A = 1;
- Include 'B. Inc ';
- ?>
The variable $ a will take effect in the included file B. inc. In a user-defined function, a local function range is introduced. By default, any variable used in a function is limited to a local function. Php global variables 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 $ GLOBALS to access global variables within the global scope. you do not need to use the global keyword to access global variables in the function. $ 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 variable ). Php static variables only exist in local function domains, but their values are not lost when the program runs out of this scope. 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. A simple example of php static variables will cause parsing errors if you assign values to the results of expressions in the declaration. 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. |