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.
First case: A files include B files, and the variables in a can be called in the B file.
A file code:
<?php $aaa = ' 123 '; Include "b.php";
B File Code:
<?phpecho $aaa;
The content can be output normally.
second case: A files include B files, and then a variable of b file can be called in a file.
A file code:
<?phpinclude "b.php"; Echo $fff;
B File Code:
<?PHP$FFF = ' I am f ';
This time, the content can be output normally.
The third case: A is called in a method of a class of a file, and a variable in the method can be called in the B file.
A file code:
<?phpclass test{public function Show () { $bbb = ' abc '; Include "b.php"; }} $t = new test; $t->show ();
B file's code:
<?phpecho $bbb;
This time, the content can be output normally.
Fourth case: A file introduces a B file through a defined function, and the variables in a are not available in the B file, but you can use the variables in the call function (display) in the a file.
A file code:
<?PHP$AAA = ' 123 '; function display ($file) { $bbb = ' Asdasdas '; Include $file;} Display ("b.php");
B File Code:
<?phpecho $aaa; Echo $bbb;
After running the $aaa prompt is undefined, $bbb can 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. The shortcomings of the text welcome correction.
The above mentioned is the whole content of this article, I hope you can like.