Examples of two functions for code reuse in PHP

Source: Internet
Author: User
The example explains two functions for code reuse in PHP: include () and require (). No matter the size of the website you are developing, you should be aware of the importance of code reuse, whether the code is HTML or PHP code. For example, you must at least

  The example explains two functions for code reuse in PHP: include () and require ()

Regardless of the size of the website you are developing, you should be aware of the importance of code reuse, whether it is HTML or PHP code. For example, you must change the footer containing copyright information at least once a year. if your website contains 1000 pages, it is annoying to modify the footer once a year.

In PHP, there are at least a few functions that can help you achieve code reuse goals. the applied functions depend on the code you reuse. the important functions are:

* Include () and include_once ()
* Require () and require_once ()

The include () function contains and calculates the given file, for example:

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

Any code in the include file is executed within the scope of the code presented by include (). you can use the combined application include () and fopen () to include static files on your own server, include the target file on another server.

Include_once () has the same efficacy as include (). The difference between the two is that it checks whether the code in a file is included in the 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 is generated during PHP engine code compilation rather than during execution, unlike include () this will be calculated 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.

In order to understand its content, I prefer to apply the require function to copyright information, static text and other elements that do not contain variables or those that are attached to other scripts being executed. For example:


Something

[A lot of content]
// Insert copyright
Require ('/home/me/mycopyright ');
?>

On the other hand, I often apply include () at the beginning of a file to hold many functions:

// Obtain the function library
Include ('/home/me/myfunctions ');
// Do PHP things with my functions?>

Something

[A lot of content]

The next title should be "where are the include and require files ?", A brief answer to this title is "any location in the system ." If your code contains a database connection with a user name and password, you are sure not to store them in the root directory of the document for everyone.

Stored Ded or required files can be stored anywhere on the system, as long as users on the system where PHP runs can visit these files, you can make these files have any suffix, or do not apply the suffix.

The application include () and require () is a broad phenomenon to embody the elements in the website, and it 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.