php中的include和require的区别_PHP教程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php中的include和require的区别_PHP教程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3379字,纯文字阅读大概需要5分钟。
内容图文
![php中的include和require的区别_PHP教程](/upload/InfoBanner/zyjiaocheng/184/19bee0e6345a47d3b3d31ac4ccdf138e.jpg)
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. |
---|
http://www.bkjia.com/PHPjc/845436.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/845436.htmlTechArticlephp中的include和require的区别 主要关注红色标记语句即可。 The include (or require) statement takes all the text/code/markup that exists in the specified file and...
内容总结
以上是互联网集市为您收集整理的php中的include和require的区别_PHP教程全部内容,希望文章能够帮你解决php中的include和require的区别_PHP教程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。