The importance analysis of PHP function include () _php tutorial

Source: Internet
Author: User
When we develop a website, we must consider the issue of code reuse, regardless of the size of your site, and regardless of the number of times you maintain and update, this problem is very troublesome. Today we will introduce you to this solution, which is the specific application of the PHP function include ().

  • , there are at least a few functions that can help you achieve the purpose of code reuse, and the functions that you use depend on the code you reuse, the main functions are:

    * Include () and include_once ()

    * Require () and require_once ()

    The PHP function include () contains and evaluates a 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 (), and you can include the target file on the other server by combining the include () and fopen () to include the static file on your own server.

    The function of include_once () is the same as include (), where the difference between the two is that it checks whether the code in one file is already contained in the existing script, and if the code already exists, it will not be included again.

    The Require () function replaces itself with the contents of a given file, which occurs during the PHP engine's compile code, rather than during execution, and is not evaluated first as a PHP function include (). The Require () function is used more in static elements, while the include () is more used in dynamic elements. Similar to Include_once (), require_once () will first check if the given code has been inserted, and if the code already exists, it will no longer be inserted.

    To also understand its content, I tend to use the Require function in copyright information, static text, and other elements that do not contain variables or that rely on other executing scripts. For example:

       
       
    1. < HTML >
    2. < HEAD > < TITLE > Something TITLE> HEAD>
    3. < BODY >
    4. [A lot of content]
    5. Insert Copyright
    6. Require ('/home/me/mycopyright ');
    7. ?>
    8. BODY >
    9. HTML >

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

       
       
    1. Get a library of functions
    2. Include ('/home/me/myfunctions ');
    3. //Do PHP things with my functions ?>
    4. < HTML >
    5. < HEAD > < TITLE > Something TITLE> HEAD>
    6. < BODY >
    7. [A lot of content]
    8. BODY >
    9. HTML >

    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 a database connection with a user name and password, you won't be able to put them all open to everyone in the document root directory," he adds.

    Included or required files can be anywhere on the system, as long as users on the system running the PHP can access the files, you can make them any suffix, or do not use suffixes.

    Using the PHP function include () and require () to externalize elements in a Web site is a common phenomenon, and it can be a great convenience when you need to upgrade your site.


    http://www.bkjia.com/PHPjc/446333.html www.bkjia.com true http://www.bkjia.com/PHPjc/446333.html techarticle when we develop a website, we must take into account the problem of code reuse, regardless of the size of your site, and no matter how many times you maintain and update, this problem is very hemp ...

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.