Home > Article > Backend Development > 10 Tips for PHP Scripting (3)_PHP Tutorial
Included files
If you are currently working on a large-scale Web site development project, then you must have a deep understanding of code reuse. For example, whether it is HTML or PHP code blocks, as long as the project is large enough, such as 1,000 Web pages , even if you only modify the footer containing copyright information once a year, it will make you miserable.
PHP can help you reuse code through some functions. The specific functions you want to use depend on the content you plan to reuse.
The main functions are:
* include() and include_once()
* require() and require_once()
include() function contains the given file, such as:
include('/home/me/myfile');
Any code in the included file will be executed within the variable scope of the code page where include() is located. . You can include static files on the server or include object files on other servers by using include() and fopen() in combination.
The include_once() function is similar to the include() function, except that this function will check whether the code in the included file has been included by the current script. If the code is already included in the script, the function no longer includes the corresponding file. The
require() function replaces itself with the contents of the given file. This replacement process occurs when the PHP engine compiles your code rather than executing it, which is different from include(), which is first calculated and added to the document. The require() function is mostly 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 already been inserted into the document. If so, the given code will not be inserted into the document again.
I suggest you use the require function for information such as copyright, static text and other elements that do not contain variables. It is also best to use the require function for elements that rely on other scripts to implement their content, for example: