Home >Backend Development >PHP Tutorial >Analysis of the Importance of PHP Function Include()_PHP Tutorial

Analysis of the Importance of PHP Function Include()_PHP Tutorial

WBOY
WBOYOriginal
2016-07-15 13:29:581345browse

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

  • , there are at least a few functions that can help you achieve the purpose of code reuse. The functions used depend on the code you are reusing. The main functions are:

    * 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.


    www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446333.htmlTechArticleWhen we develop a website, we must consider the issue of code reuse, no matter the size of your website , no matter how many times you maintain and update, this problem is very troublesome...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn