This article mainly introduces the solution to the global variable global failure after multiple includes in PHP. The example analyzes the principle and solution of $GLOBALS failure, 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.
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 caused by 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.
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
php operates strings and arrays to implement similarity algorithm
php regular expression to implement filtering UBB Code class
php method to operate ffmpeg to add subtitles to video
##
The above is the detailed content of The principle and solution of $GLOBALS failure in PHP. For more information, please follow other related articles on the PHP Chinese website!