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.
Series Contains failure Features
# 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:
<html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <?php include 'header.php'; ?> <h1>欢迎来到我的主页!</h1> <p>一些文本。</p> </body> </html>
Example 2
Suppose we have a standard menu file used in all pages.
"menu.php":
echo '<a href="/">Homepage</a>
"/html">HTML Tutorial</a>
#All pages in the website should reference this menu file. The following is the specific approach: <html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<div class="leftmenu">
<?php include 'menu.php'; ?>
</div>
<h1>欢迎来到我的主页!</h1>
<p>一些文本。</p>
</body>
</html>
Example 3
Suppose we have an include file ("vars.php") that defines variables:
<?php $color='red'; $car='BMW'; ?>
These variables can be used in calls In the file:
<html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <h1>欢迎来到我的主页!</h1> <?php include 'vars.php'; echo "I have a $color $car"; // 输出 I have a red BMW ?> </body> </html>
Example 4
Create a file.php file:
<?php $word = "你好!"; ?>
Then include it in another file such as test.php (the two files are in the same Directory):
<?php echo "包含内容为:".$word."<br />"; 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_once function There is no difference in function. The main function is to avoid repeated inclusion of