PHP Global variables define the global variable implementation of the current page

  • 2020-06-07 04:06:35
  • OfStack

In the practical application of PHP Global variables, we will find many problems that need to be improved constantly. In this article, we present some specific solutions to the problems with the PHP Global variable.

1: The PHP Global variable is used to define the global variable, but this global variable is not applied to the entire site, but to the current page, including all files in include or require


$a=123; 

function aa() 
{ 
Global $a; 
// If you don't $a Is defined as global variable  
, The function body is not accessible $a the  
echo $a; 
} 
aa(); 

Conclusion: PHP Global variables defined in the body of the function can be used outside the body of the function, while global variables defined in the body of the function cannot be used in the body of the function.
 
$glpbal $a; $a=123; function f() { echo $a; // error , } 

Let's look at the next example
 
function f() 
{ 
global $a; 
$a=123; 
} 
f(); 
echo $a; // correct , You can use  

2. Analysis of PHP Global variable:
question: I defined 1 variable in config.inc.php ($a), in other files include(" config.inc.php ") outside the function. I need to use the variable $a inside the function. So global $a is declared, but there are a lot of functions and a lot of variables, so you can't just declare it over and over again, right? If there is any good solution, please advise.

answer1: Define the constant in config.inc.php: define(name of the constant, value of the constant) and use it wherever you need it. Then you can use the constant directly in this file.
answer2: I also have a way to define arrays, such as $x[a], $x, and then just declare global $x1.
answer3: I tried your method and it doesn't work.
answer4: Change your ES62en.ini file.
Set the PHP Global variable to on


Related articles: