Home  >  Article  >  Backend Development  >  PHP Global variables define global variables of the current page_PHP tutorial

PHP Global variables define global variables of the current page_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:32:431083browse

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

  1. $a=123;
  2. {
  3. Global $a;
  4. //If $a is not defined as a global variable
  5. , $a cannot be accessed within the function body
  6. echo $a;
  7. }
  8. aa();
  9. 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>
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>

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn