Home > Backend Development > PHP Tutorial > 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
Release: 2016-07-13 10:05:47
Original
1118 people have browsed it

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;>
Copy after login

b.php file:

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

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;>
Copy after login

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...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template