Importance Analysis of PHP function include ()

Source: Internet
Author: User

When developing a website, we must consider the issue of code reuse, regardless of the size of your website or the number of maintenance and updates, this problem is very troublesome. Today, we will introduce you to 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 reuse. The main functions are:

    * Include () and include_once ()

    * Require () and require_once ()

    The PHP function include () contains and computes the given file. For example:

    Include ('/home/me/myfile ');

    Any code in the include file is executed within the scope of the Code that appears in the PHP function include (). You can use include () and fopen () together () include static files on your server and target files on another server.

    Include_once () has the same function as include (). The difference between them is that it checks whether the code in a file is included in an existing script. If the Code already exists, it will not be included again.

    The require () function replaces itself with the content of a given file, which occurs during PHP engine code compilation rather than during execution, unlike the PHP function include () the calculation is performed first. The require () function is more used in static elements, while the include () function is more used in dynamic elements. Similar to include_once (), require_once () first checks whether the given code has been inserted. If the Code already exists, it is no longer inserted.

    To learn more about the content, I prefer to use the require function in copyright information, static text, and other elements that do not contain variables, or those that depend on other scripts being executed. For example:

       
       
    1. <HTML>   
    2.  
    3. <HEAD><TITLE>Something</TITLE></HEAD>   
    4.  
    5. <BODY>   
    6.  
    7. [a lot of content]   
    8.  
    9. <?   
    10.  
    11. // insert copyright   
    12.  
    13. require('/home/me/mycopyright');   
    14.  
    15. ?>   
    16.  
    17. </BODY>   
    18.  
    19. </HTML>  

    On the other hand, I often use the PHP function include () to control many functions at the beginning of the file:

       
       
    1. <?
    2.  
    3. // Obtain the function library
    4.  
    5. Include ('/home/me/myfunctions ');
    6.  
    7. // Do PHP things with my functions?>
    8.  
    9. <HTML>
    10.  
    11. <HEAD><TITLE>Something</TITLE></HEAD>
    12.  
    13. <BODY>
    14.  
    15. [A lot of content]
    16.  
    17. </BODY>
    18.  
    19. </HTML>

    The next question should be "Where are the include and require files ?", A simple answer to this question is, "anywhere in the system ." If your code contains a database connection with a user name and password, you will not put them in the root directory of the document to be open to everyone.

    Stored ded or required files can be stored anywhere on the system, as long as users on the system where PHP runs can access these files, you can make these files have any suffix, or do not use a suffix.

    Using the PHP functions include () and require () to externalize the elements on the website is a common phenomenon and brings great convenience to you when you need to upgrade the website.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.