Code reuse through the Include and Require functions _ PHP Tutorial

Source: Internet
Author: User
Use the Include and Require functions to reuse code. If you have made Web sites of various sizes, you will have a deep understanding of the importance of reusing code segments, whether it is HTML or PHP code blocks. For example, you need to modify the page containing the copyright information once a year. if you have made Web sites of various sizes, you will have a deep understanding of the importance of reusing code segments, whether it is HTML or PHP code blocks. For example, if you need to modify the footer containing the copyright information once a year and you have 1000 Web pages (even 10), manual operations will make you suffer. With PHP, you can use different functions to help you reuse code. The specific function used depends on the content you intend to reuse. The main function is as follows: * include () and include_once () * require () and require_once () include () function contains the given file and checks it. For example: include (/home/me/myfile); any code in the contained file will be executed within the variable range, which is equivalent to the function include () in the parent code. You can use the include () and fopen () functions in combination to include static files on the server or target files on the other server. The include_once () and include () functions perform the same functions. they only check whether the code in the file has been included in the current script. If the code has been included by the script, the function will no longer contain the corresponding files. The require () function replaces itself with the content of the given file. This replacement process occurs when the PHP engine compiles your code instead of the execution phase. Unlike include (), the latter is first computed. The require () function is more used for static elements, while the include () function is mainly used for dynamic elements. Similar to include_once (), the require_once () function checks whether the given code has been inserted into the document. if the code already exists, the given code will not be inserted into the document again. We recommend that you use the require function for information such as copyright, static text, and other elements without Variables. The require function should also be used for elements that depend on other scripts to implement their own content. for example: Something [A lot of content] On the other hand, the include () function is often used to put the function library or similar libraries out of the script: Something [A lot of content] The next natural question is: "Where are the files that have been added or that have been required from ?" The simple answer is: "your system ." However, some code snippets sometimes contain security information such as database connections that contain user names and passwords. Obviously, you do not want these things to be made public as part of the document. You can place the included files (encoded or required) anywhere in the system, as long as the user's PHP can access these files. You can also give these files any extension you want, or do not have any extension at all. The include () and require () functions can be used to externalize elements that are common or frequently changed on the Web site. This method also makes the system architecture easier to handle when upgrading is required.

Bytes. For example, you need to modify the page containing the copyright information once a year...

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.