PHP include files
In our actual production work, when building a larger system, there are always some contents that need to be reused, such as some commonly used functions, or some common html elements such as menus, footers, etc. We can write these public contents into some files together, and then include them where needed according to the specific situation. This can save a lot of development time and make the code files unified and concise for better maintenance.
In PHP, there are four methods: require, require_once, include, and include-once to include a file.
SeriesContains failureFeatures
# Inlcude Return a warning file to continue executing down. Usually used for dynamic inclusion.
Require. A fatal error. The code will not continue to execute. Usually extremely important files are included, and the entire code should not be executed
Include_once Returns a warning In addition to the original include function, it will also perform once detection. If the file has been included before, it will no longer be included
Require_once A fatal error In addition to the original function 1, a once detection will be done to prevent files from being included repeatedly
PHP include and require statements
In PHP, you can insert the contents of a file into the PHP file before it is executed by the server.
include and require statements are used to insert useful code written in other files into the execution flow.
include and require are identical except for how they handle errors:
require generates a fatal error (E_COMPILE_ERROR) after which the script stops executing.
include generates a warning (E_WARNING) and the script will continue execution after the error occurs.
So if you want to continue execution and output the results to the user even if the included file is missing, then use include. Otherwise, in frameworks, CMS, or complex PHP application programming, always use require to reference key files to the execution flow. This helps improve application security and integrity in the event that a critical file is accidentally lost.
Including files saves a lot of work. This means you can create standard header, footer or menu files for all web pages. Then, when the header needs updating, you simply update the header include file.
grammar
include 'filename';
or
require 'filename';
Basic example
Suppose you have a standard page header file named "header.php". To reference this header file in the page, please use include/require:
php中文网(php.cn) 欢迎来到我的主页!
一些文本。
Example 2
Suppose we have a standard menu file used in all pages.
"menu.php":
echo 'Homepage
"/html">HTML Tutorial
#All pages in the website should reference this menu file. The following is the specific approach:
Example 3php中文网(php.cn) 欢迎来到我的主页!
一些文本。
Suppose we have an include file ("vars.php") that defines variables:
These variables can be used in calls In the file:
php中文网(php.cn) 欢迎来到我的主页!
Example 4
Create a file.php file:
Then include it in another file such as test.php (the two files are in the same Directory):
"; include("file.php"); echo "包含内容为:".$word; ?>
Run test.php and the output is as follows:
Contains the content:Contains the content: Hello!
The difference between include() and require()
require() statement can also be used to include files. Equivalent to include() in use. But there are some subtle differences between the two. You can use include() or require() depending on the actual situation.The difference between the two is as follows:
When the included file does not exist (an error occurs in the inclusion), if require() is used, the program will stop executing immediately, while if include() is used, the system In addition to prompting errors, the following program content will continue to execute. In most cases, it is recommended to use the require() function to prevent the program from continuing to execute after an error reference occurs
Regardless of whether the require() statement is executed, the program execution include files are added, and include() only executes Only then will the file be included. Therefore, if there is a conditional judgment, it is obviously more appropriate to use include().
When using require() for multiple references, the reference action to the referenced file will only be executed once, while include() will be used every time. The reference file must be read and evaluated
onceinclude and require respectively correspond to a once function: include_once and require_oncefunction There is no difference in function. The main function is to avoid repeated inclusion of