Code reuse through include and require functions

Source: Internet
Author: User
If you've done Web sites of all sizes, it's important to be aware of the importance of reusing code snippets, whether it's HTML or PHP code blocks. For example, it takes a year to modify a footer that contains copyright information, and you have 1000 Web pages (even 10), and manual operations can be painful.
With PHP, you can use different functions to help you reuse code. The specific function depends on what you intend to reuse.
The main functions are as follows:
* Include () and include_once ()
* Require () and require_once ()
The Include () function contains the given file and checks it. Such as:
Include ('/home/me/myfile ');
Any code in the contained file will be executed within the scope of the variable, which is equivalent to where the function include () appears in the parent code. You can use the include () and fopen () functions to include a static file on a server or a target file on another server.
The include_once () function completes the same function as the include () function, examining only whether the code within the file has been included by the current script. If the code is already contained by the script, the function no longer contains the corresponding file.
The Require () function replaces itself with the contents of a given file. This substitution process occurs when the PHP engine compiles your code and is not in the execution phase, unlike include (), which is first computed. The Require () function is used more for static elements, while the include () function is used primarily for dynamic elements. Similar to Include_once (), the require_once () function checks whether a given code has already been inserted into the document, and if the code already exists, the given code will not be inserted again.
It is recommended that you use the Require function for information such as copyrights, static text, and other elements that do not contain variables, and that elements that rely on other scripts to implement their own content should also take the Require function, for example:
<HTML>
<HEAD><TITLE>Something</TITLE></HEAD>
<BODY>
[A lot of content]
?
Insert Copyright information
Require ('/home/me/mycopyright ');
?>
</BODY>
</HTML>
On the other hand, you often use the Include () function to put a library of functions or similar libraries outside the script:
?
get my library of functions
Include ('/home/me/myfunctions ');
Use my function to execute a PHP program? >
<HTML>
<HEAD><TITLE>Something</TITLE></HEAD>
<BODY>
[A lot of content]
</BODY>
</HTML>
The next natural question is: "Where are the documents that were included or required?" "The simple answer is:" Your system. "However, just some snippets of code sometimes contain security information such as a database connection that determines the user name and password." Obviously you don't want these things to become part of the document and be made public.

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.