Include 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, you Changing the footer containing copyright information even once a year can be a pain in the ass.
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 includes the given file, such as:
include(/home/me/myfile);
Any code in the include 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 own content, such as:
You will definitely ask: "So, where do the included or required files come from?" The answer is simple: "Your system." However, sometimes, some code will contain files such as files with user names and passwords. Security information such as database connection functions, in this case, obviously, you definitely don't want these things to become part of the document and become known to the whole world.
You can place included files (included or required) anywhere in the system, as long as the PHP available to users can access these files. You can also give these files any file extension you wish, or no extension at all.
Using the include() and require() functions can make elements that are common or frequently changing on the website easier to handle.