The role of the 1:php global variable is to define global variables, but this global variable is not applied to the entire site, but to the current page, including all files of include or require
- $ a = 123 ;
- function AA ()
- {
- Global $a;
- If you do not define $ A as the global variable
, the function body is not accessible to $ A
- echo $a;
- }
- AA ();
Summary: The PHP global variable defined in the function body can be used outside the body, and the global variable defined outside the function body cannot be used within the function body.
- $glpbal $a;
- $ a = 123 ;
- function f ()
- {
- echo $a; Error
- }
Take a look at the following example
- function f ()
- {
- Global $a;
- $ a = 123 ;
- }
- f ();
2:php global variable Problem Resolution:
Question: I defined some variables ($a) in the config.inc.php, and in other files the function external include ("config.inc.php"), the function internal need to use these variable $ A, if there is no declaration, echo $ A is not printing anything. So declaring the global $a, but there are a lot of functions and a lot of variables, you can't always repeat this statement? If there is any good solution, please advise.
Answer1: Define constants in config.inc.php first: define (constant name, constant value), and then require ' config.inc.php ' in other places where it is needed, and then you can use the constant in this file directly.
Answer2: I also have a way of defining arrays, such as $x[a], $x, so just declare the global $x one.
Answer3: I tried this method of yours, No.
Answer4: Change your php.ini file.
Set the PHP global variable to ON
http://www.bkjia.com/PHPjc/446106.html www.bkjia.com true http://www.bkjia.com/PHPjc/446106.html techarticle the role of the 1:php global variable is to define global variables, but this global variable is not applied to the entire site, but to the current page, including all files of include or require $ A = 123 ...