PHP include files
In PHP, you can insert the contents of a file into the PHP file before it is executed by the server. The
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), The script will stop executing after an error occurs.
include Generate 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 in the execution flow (from one statement to the next, until the end of the program is run). 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 pages. Then, when the header needs updating, you simply update the header include file.
Syntax
##include "filename";
orrequire "filename";
##Instance
Assume there is a file named header.php, as follows
<?php header("Content-type:text/html;charset=utf-8"); //设置编码 $name="PHP中文网"; $php="PHP"; ?>
Use include to include the header.php file
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>PHP中文网</title> </head> <body> <?php include "header.php"; echo "欢迎来到"."$name"."学习"."$php"; ?> </body> </html>
Program running result:
Welcome to the PHP Chinese website to learn PHP
Instance
There is one Standard menu file named "menu.php":
<?php header("Content-type:text/html;charset=utf-8"); //设置编码 echo '<a href="/index.asp">首页</a> - <a href="/html/index.asp">HTML 教程</a> - <a href="/css/index.asp">CSS 教程</a> - <a href="/js/index.asp">JavaScript 教程</a> - <a href="/php/index.asp">PHP 教程</a>'; ?>
Use "require" to include the "menu.php" file
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>PHP中文网</title> </head> <body> <h1>欢迎来到PHP中文网</h1> <?php require "header.php"; ?> </body> </html>
Program Running results:
Welcome to PHP Chinese websiteHomepage-HTML Tutorial-CSS Tutorial-JavaScript Tutorial-PHP Tutorial
include VS require##There is a huge difference between include and require: if a file is referenced with an include statement and PHP cannot find the file, the script will continue execution
.If we use the require statement to complete the same case, the echo statement will not continue execution because the script will terminate execution after the require statement returns a serious error
Example
## Use include to include non-existent files
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>PHP中文网</title> </head> <body> <h1>欢迎来到PHP中文网</h1> <?php include "noFile.php"; echo "我在学"."$name"; ?> </body> </html>Program running result:
Welcome to PHP Chinese website
I'm learning
Use require to include non-existent files
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>PHP中文网</title> </head> <body> <h1>欢迎来到PHP中文网</h1> <?php require "noFile.php"; echo "我在学"."$name"; ?> </body> </html>
Program running result:
Welcome to PHP Chinese websiteAttention
: When using include and require to include a non-existent file, a warning message may appear. This is the error level you can set in php.ini
- Open php.ini
- Ctel+F search error_reporting
- Find error_reporting = E_ALL
- Replace error_reporting = E_ALL into error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_WARNING
- ##Restart Apache
##