Solution to the failure of the global variable global after multiple PHP includes_PHP Tutorial

WBOY
Release: 2016-07-13 10:05:44
Original
809 people have browsed it

The solution to the global variable global invalidation after multiple PHP includes

This article mainly introduces the solution to the global variable global invalidation after multiple PHP includes, example analysis The principle and solution of $GLOBALS failure are explained, which has certain reference value. Friends in need can refer to it

The example in this article describes the solution to the failure 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:

?

1

2

3

$aa = 1;

?>

1 2

3

$aa = 1;

1

2

3

4

5

6

7

include a.php

function show(){

global $aa;

var_dump($aa);

}

?>

?>

b.php file:

?

1

2

3

$GLOBALS['aa'] = 1;

?>

1 2

3

4

5 6

7
include a.php function show(){ global $aa; var_dump($aa); }

?>
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: ?
1 2 3 <🎜>$GLOBALS['aa'] = 1;<🎜> <🎜>?>
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. http://www.bkjia.com/PHPjc/962406.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/962406.htmlTechArticleThe solution to the failure of the global variable global after multiple PHP includes The solution to the failure of global variables, an example analysis of $GLOBALS failure...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!