Home  >  Article  >  Backend Development  >  Solution to the failure of the global variable global after multiple includes in PHP, global variable global_PHP tutorial

Solution to the failure of the global variable global after multiple includes in PHP, global variable global_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:05:471064browse

The solution to the global variable global becoming invalid after multiple includes in PHP. The global variable global

The example in this article describes the solution to the invalidation of the global variable global after multiple includes in PHP. Share it with everyone for your reference. The specific analysis is as follows:

In multiple files, the files are included one after another, but the function in the last file cannot reference global variables after using global. For example:
a.php file:

<&#63;php
$aa = 1;
&#63;>

b.php file:

<&#63;php
include a.php
function show(){
global $aa;
var_dump($aa);
}
&#63;>

Display: null;

This failure is due to a variety of reasons. An effective approach is to use the $GLOBALS array if you decide to use a variable as a global variable for multiple files. For example, a.php in the above example:

<&#63;php
$GLOBALS['aa'] = 1;
&#63;>

Then you can reference this variable in functions and methods in multiple files.

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/962097.htmlTechArticleThe solution to the failure of the global variable global after multiple PHP includes. The global variable global. This article tells the example of PHP multiple times. The solution to the global variable global invalidation after include. Share to...
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