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:
<?php $aa = 1; ?>
b.php file:
<?php 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:
<?php $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.