If you have made Web sites of various sizes, you will surely 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 footer containing the copyright information once a year, and you have 1000 Web pages (even 10), manually
If you have made Web sites of various sizes, you will surely 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 copyright information once a year and you have 1000 Web pages (even 10), manual control will make you suffer.
With PHP, you can apply 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 scope of the variable, which is equivalent to the place where the function include () is presented in the parent code. You can combine the include () and fopen () functions to include static files on the server or target files on the other server.
The include_once () and include () functions have the same effect. 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 applies the content of a given file to change itself. This swap process is generated when the PHP engine compiles your code, instead of the fulfillment phase. this is different from include (), which is first calculated. The require () function is more used for static elements, while the include () function is important 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 that do not include variables. The require function should also be used for elements that implement their own content by attaching other scripts. 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 ');
// Apply my function to the PHP program?>
Something
[A lot of content]
The next natural title is: "Where are the files that are encoded or required from ?" The simple answer is: "your system ." However, some code snippets sometimes contain security information such as database connections that determine user names and passwords. Obviously, you don't want to read these things into the public as part of the document.
You can place the included files (encoded or required) in any place of the system, as long as the user's PHP can visit these files. You can also give these files an extended name that you want to see, or you do not have an extended name.