Implementing code reuse with include and require functions

Source: Internet
Author: User

Implementing code reuse with include and require functions

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, if you need to revise a footer that contains copyright information once a year, and you have 1000 Web pages (even 10), manual actions can make you miserable. With PHP, you can use different functions to help you reuse your code.      The specific function you use 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 together to include static files on the server or target files on another server. The include_once () function accomplishes the same function as the include () function, checking that only the code within the file has been included in the current script.      If the code is already included in the script, the function will no longer contain 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 rather than the execution phase, which differs from the include (), which is computed first. The Require () function is more used for static elements, while the include () function is primarily used for dynamic elements.      Similar to Include_once (), the require_once () function checks that the given code has already been inserted into the block, and if the code already exists, the given code will not be inserted again in the document. It is recommended that the Require function be used 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] <? // require ( ' /home/me/mycopyright '

On the other hand, you often use the Include () function to place libraries or similar libraries outside of a script:

  <?    //Get my function Include‘/home/me/myfunctions‘);//Use my functions to execute PHP programs? >    <Html>    <head><< Span style= "color: #000000;" >title>something </title></ Head> <body> [a lot of content] </body> </html>

The next natural question is: "Where are the files that were included or required from?" "The simple answer is:" Your system. "However, just a few snippets sometimes contain security information such as a database connection with a user name and password.      Obviously you don't want these things to become part of the document and be made public. You can put the included files (included or required) anywhere on the system, as long as the user's PHP can access the files.      You can also give these files any extensions you want, or no extensions at all. Use the include () and require () functions to externalize elements that are ubiquitous or frequently changing on a Web site. This approach also makes the system architecture easier to handle when upgrades are needed.

Implementing code reuse with include and require functions

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.