Global is a global variable in php. This global is only a global variable on the page. for example, we can directly use the global declaration in the function to obtain the value of the variable. The instance code is as follows :? Php $... global is a global variable in php. This global is only a global variable on the page. for example, we can directly use the global declaration in the function to obtain the value of the variable.
The instance code is as follows:
Output result: $ a = 24
Conclusion: 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. the code is as follows:
The above instance is just a basic knowledge of global variables. let's take a look at the complex points. The code is as follows:
// A. php file
// B. php file
Why is the output 0 ?!!
In a user-defined function, a local function range is introduced. Any variables used in the function will be limited to the local function scope by default (including the variables in the include and require imported files )!
Explanation:. test_Global in the PHP file is a defined third-party function, which is imported into B using include. the global variable of $ a in the PHP file, so $ a is restricted to the range of local functions of Test_Global, so B. $ a in the PHP file takes effect in Test_Global, instead of. php ....
Solution:
1. execute the local function. the code is as follows:
// A. php file
// B. php file
Global problem analysis:
Question: In config. inc. php defines some variables ($ a). in other files, the function external include ("config. inc. php "), the function needs to use these variables $ a internally. If no declaration is made, echo $ a cannot print anything. Therefore, we declare global $ a. But there are a lot of functions and many variables that cannot be repeatedly declared? Please advise if you have any good solutions.
Answer1: first define a constant in config. inc. php: define (constant name, constant value)
Then, in other places that need to be used, require 'config. inc. php ',
Then you can directly use this constant in this file.
Answer2: I also have a way to define an array, such as $ x [a], $ x, so that we only need to declare a global $ x.
Answer3: I tried your method. no.
Answer4: modify your php. ini file.
Address:
Reprinted at will, but please attach the article address :-)