In PHP we sometimes need to include a file. For example, I used to write a framework, I intend to use the original PHP as a template, and then write a display method to introduce template file, but this is just my fantasy.
After writing, it is found that all variables in the template are not defined. The scope of the include file is summarized in several cases through various research and search data.
The first case: A file include B file, in the B file cannot call a middle variable.
a file code :
<? var$aaa = ' 123 '; include "B.php";
b File Code :
<? PHP Echo $aaa;
This time will prompt $aaa undefined.
The second case: A file include B file, and then in a file you can call the B file variable.
a file code :
<? PHP include "b.php"; Echo $fff;
b File Code :
<? PHP $fff = ' I am f ';
This time, the content can be output normally.
In the third case, a method in a class of a file invokes the B file, and then the variable in the method can be called in the B file.
a file code :
<? PHP class test{ publicfunction Show () { var$bbb = ' abc '; include "b.php"; }} $t New test; $t->show ();
b file's code :
<? PHP Echo $bbb;
This time, the content can be output normally.
So it's not feasible for me to start by using a display method to introduce a template. Based on three scenarios, I finally chose to write a class to import the template file. At present, thinkphp and Smarty also use classes to introduce template files.
Research on the scope of include file variables in PHP