When we develop a website, we must consider the issue of code reuse. No matter the size of your website or the number of times you maintain and update it, this problem is very troublesome. Today we will introduce to you a solution to this problem, which is the specific application of the PHP function include(). In
* include() and include_once()
* require() and require_once()
The PHP function include() includes and performs calculations on a given file, for example:
include('/home/me/myfile');
Any code in the include file is executed within the code scope where the PHP function include() appears. You can use include() and fopen() includes static files on its own server and target files on another server.
include_once() has the same function as include(). The difference between the two is that it checks whether the code in a file is already included in an existing script. If the code already exists, it will not Include it again. The
require() function replaces itself with the contents of the given file. This replacement process occurs when the PHP engine compiles the code, not during execution. It does not first Make calculations. The require() function is more used in static elements, while include() is used more in dynamic elements. Similar to include_once(), require_once() will first check whether the given code has been inserted. If the code already exists, it will not be inserted again.
In order to understand its content, I prefer to use the require function in copyright information, static text and other elements that do not contain variables or rely on other executing scripts. For example:
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>HTML</SPAN><SPAN class=tag>></span></font></strong><span> </span></span></li> <li class=""><span> </span></li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>HEAD</SPAN><SPAN class=tag>></span><span class="tag"><</SPAN><SPAN class=tag-name>TITLE</SPAN><SPAN class=tag>></span></font></strong><span>Something</span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>TITLE</SPAN><SPAN class=tag>></span><span class="tag"></</SPAN><SPAN class=tag-name>HEAD</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>BODY</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class=""><span> </span></li> <li class="alt"><span>[a lot of content] </span></li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699"><?</FONT></STRONG></SPAN><SPAN> </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>// insert copyright </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>require('/home/me/mycopyright'); </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></font></strong></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>BODY</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>HTML</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> </ol>
On the other hand, I often The PHP function include() is used at the beginning of the file to control many functions:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><strong><font color="#006699"><?</FONT></STRONG></SPAN><SPAN> </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>//得到函数库 </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>include('/home/me/myfunctions'); </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>// do PHP things with my functions </SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></font></strong></span><span> </span></span></li> <li class=""><span> </span></li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>HTML</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>HEAD</SPAN><SPAN class=tag>></span><span class="tag"><</SPAN><SPAN class=tag-name>TITLE</SPAN><SPAN class=tag>></span></font></strong><span>Something</span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>TITLE</SPAN><SPAN class=tag>></span><span class="tag"></</SPAN><SPAN class=tag-name>HEAD</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>BODY</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class=""><span> </span></li> <li class="alt"><span>[a lot of content] </span></li> <li class=""><span> </span></li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>BODY</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>HTML</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> </ol>
The next question should be "Where are the include and require files?" The simple answer to this question is, "Anywhere in the system." If your code contains There are database connections with usernames and passwords, and you certainly wouldn't put them all in the document root and make them available to everyone.
Included or required files can be anywhere on the system, as long as they are accessible to users on the system where PHP is running. You can have these files with any suffix, or use no suffix.
Using the PHP functions include() and require() to specify elements in a website is a common phenomenon and will bring you great convenience when you need to upgrade the website.