1: The function of PHP Global variables is to define global variables, but this global variable does not apply to the entire website, but to the current page, including include or require all files
- $a=123;
{ -
Global $a; -
//If $a is not defined as a global variable- , $a cannot be accessed within the function body
- echo $a;
- }
aa();
- Summary: PHP Global variables defined inside the function body can be used outside the function body. Global variables defined outside the function body cannot be used outside the function body. For internal use,
Look at the following example
<ol class="dp-xml">
<li class="alt"><span><span>$glpbal $a; </span></span></li>
<li>
<span>$</span><span class="attribute">a</span><span>=</span><span class="attribute-value">123</span><span>; </span>
</li>
<li class="alt"><span> </span></li>
<li><span>function f() </span></li>
<li class="alt"><span>{ </span></li>
<li><span>echo $a; //错误, </span></li>
<li class="alt"><span>} </span></li>
</ol>
Copy after login
2: PHP Global variable problem analysis:
question: I defined some variables ($a) in config.inc.php, and included ("config.inc.php") outside the function in other files. , these variables $a need to be used inside the function. If not declared, echo $a will not print anything. Therefore, global $a is declared, but there are many functions and many variables. You can't declare it like this repeatedly, right? If there is any good solution, please give me some advice.
answer1: First define the constants in config.inc.php: define (constant name, constant value), then require 'config.inc.php' in other places where it needs to be used, and then you can use it here This constant is used directly in the file.
answer2: I also have a way, which is to define an array, such as $x[a], $x, so that you only need to declare global $x.
<ol class="dp-xml">
<li class="alt"><span><span>function f() </span></span></li>
<li><span>{ </span></li>
<li class="alt"><span>global $a; </span></li>
<li>
<span>$</span><span class="attribute">a</span><span>=</span><span class="attribute-value">123</span><span>; </span>
</li>
<li class="alt"><span>} </span></li>
<li><span> </span></li>
<li class="alt"><span>f(); </span></li>
<li><span>echo $a; //正确,可以使用 </span></li>
</ol>
Copy after login
answer3: I tried your method and it didn’t work.
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
1: The function of PHP Global variables is to define global variables, but this global variable does not apply to the entire website, but Applies to the current page, including all files include or require $a = 123...