php中的include和require的区别
主要关注红色标记语句即可。
The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement.
Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.
PHP include and require Statements
It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement.
The include and require statements are identical, except upon failure:
-
require will produce a fatal error (E_COMPILE_ERROR) and stop the scriptinclude will only produce a warning (E_WARNING) and the script will continue
So, if you want the execution to go on and show users the output, even if the include file is missing, use the include statement. Otherwise, in case of FrameWork, CMS, or a complex PHP application coding, always use the require statement to include a key file to the flow of execution. This will help avoid compromising your application's security and integrity, just in-case one key file is accidentally missing.
Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file.
Syntax
include 'filename';or
require 'filename';
PHP include Examples
Example 1
Assume we have a standard footer file called "footer.php", that looks like this:
echo "Copyright © 1999-" . date("Y") . " W3Schools.com
";?>
To include the footer file in a page, use the include statement:
Example
Welcome to my home page!
Some text.
Some more text.
Run example ?
Example 2
Assume we have a standard menu file called "menu.php":
echo 'Home -HTML Tutorial -
CSS Tutorial -
JavaScript Tutorial -
PHP Tutorial';
?>
All pages in the Web site should use this menu file. Here is how it can be done (we are using a
element so that the menu easily can be styled with CSS later):
Example
Welcome to my home page!
Some text.
Some more text.
Run example ?
Example 3
Assume we have a file called "vars.php", with some variables defined:
$color='red';$car='BMW';
?>
Then, if we include the "vars.php" file, the variables can be used in the calling file:
Example
Welcome to my home page!
echo "I have a $color $car.";
?>
Run example ?
PHP include vs. require
The require statement is also used to include a file into the PHP code.
However, there is one big difference between include and require; when a file is included with the include statement and PHP cannot find it, the script will continue to execute:
Example
Welcome to my home page!
echo "I have a $color $car.";
?>
Run example ?
If we do the same example using the require statement, the echo statement will not be executed because the script execution dies after the require statement returned a fatal error:
Example
Welcome to my home page!
echo "I have a $color $car.";
?>
Run example ?
Use require when the file is required by the application. Use include when the file is not required and application should continue when file is not found. |

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

WebStorm Mac版
好用的JavaScript開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境