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. For specific functions used, if you have used Web sites of various sizes, you will have a deep understanding of the importance of reusing code segments, whether 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 ()
The include () function contains the specified 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 where the function include () appears 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]
// Insert the copyright information
Require ('/home/me/mycopyright ');
?>
On the other hand, the include () function is often used to put the function library or similar libraries out of the script:
// Obtain my function library
Include ('/home/me/myfunctions ');
// Use my function to execute the PHP program?>
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.
Julie Meloni is the technical director of i2i Interactive and a major contributor to the Linux and open source communities. She has written many books on PHP and other technologies and has become an expert with long-term contributions to CNET Builder.com.