In PHP, we sometimes need to include a file. For example, I spent some time in writing a framework, I intend to use native PHP as a template, and then write a display method to introduce template files can, but this is just my imagination.
When you finish writing, you find that all the variables in the template are not defined. Through a variety of research and search data, summed up the include file in several cases of the scope.
In the first case: A file include B file, in the B file can call the variables in a.
A file code:
<?php
$aaa = ' 123 ';
Include "b.php";
B File Code:
Can output the content normally.
In the second case: A file include B file, and then in a file, you can call the variables of the B file.
A file code:
<?php
include "b.php";
Echo $fff;
B File Code:
This time it is normal to output the content.
In the third case: A file in a method of a class that calls the B file, and then in a B file, you can call the variable in the method.
A file code:
<?php
class test{public
function Show () {
$bbb = ' abc ';
Include "b.php";
}
}
$t = new test;
$t->show ();
The code for the B file:
This time it is normal to output the content.
The fourth situation: A file introduces the 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 a file called functions (display).
A file code:
<?php
$aaa = ' 123 ';
function display ($file) {
$bbb = ' Asdasdas ';
Include $file;
}
Display ("b.php");
B File Code:
<?php
echo $aaa;
Echo $bbb;
$AAA prompts are undefined after running, $BBB can output normally.
So I started with a display method to introduce the template is not feasible. According to the three kinds of situations, I finally chose to write a class to import the template file. Currently thinkphp and Smarty also use classes to introduce template files. The deficiencies in the article are welcome to correct me.
The above mentioned is the entire content of this article, I hope you can enjoy.