Home >Backend Development >PHP Tutorial >In-depth understanding of the differences between require, require_once, include and include_once_PHP Tutorial
PHP has the characteristics of fast, reliable, cross-platform application, and open source code, making PHP one of the most popular server-side Script languages. Based on what I have experienced at work, I will introduce to you my experience in using PHP. I hope it will be helpful to everyone.
Use PHP's Include files to maintain your website
No matter the size of the website you develop is large or small, you should realize the importance of reusing program code , whether you are reusing PHP programs or HTML source code. For example, the copyright notice at the end of the website page must be revised at least once a year. What should you do if your website has many pages? It must be a headache to modify these pages one by one. With PHP we can reuse program code in several different ways. Which functions to use depends on what kind of content you want to reuse.
These main functions include:
* include() and include_once()
* require() and require_once()
1.include() function will read the specified file and execute the program inside.
For example: include('/home/me/myfile');
The program code in the imported file will be executed, and when executed, these programs will have and call the source file The include() function is located in the same variable scope. You can import static files from the same server, or even import files from other servers by combining the include() and fopen() functions.
2. The function of include_once() is almost the same as include()
The only difference is that the include_once() function will first check whether the file to be imported is already in the program has been imported elsewhere, if so, the file will not be imported again (this function is sometimes very important, for example, the file to be imported declares some functions that you have defined yourself, then if If you import this file repeatedly in the same program, an error message will occur during the second import, because PHP does not allow functions with the same name to be declared a second time).
3.require() function will read the contents of the target file and replace itself with the read contents.
This reading and substitution action occurs when the PHP engine compiles your program code, not when the PHP engine starts executing the compiled program code (the way the PHP 3.0 engine works) It compiles and executes one line at a time, but this has changed with PHP 4.0. PHP 4.0 first compiles the entire program code, and then executes the compiled program code at once. During the compilation process, nothing will be executed. program code). require() is usually used to import static content, while include() is suitable for importing dynamic program code.
4. Like the include_once() function , the require_once() function will first check whether the content of the target file has been imported before. If so, it will not import the same file again. content.
I personally use the require() function to import copyrights, static text, or other program code that does not contain variables, or that needs to rely on other executed programs to execute correctly. For example: