php中的include跟require的区别

php中文网
发布: 2016-06-13 12:01:24
原创
984人浏览过

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 script
  • include 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.

PHP轻论坛
PHP轻论坛

简介PHP轻论坛是一个简单易用的PHP论坛程序,适合小型社区和个人网站使用。v3.0版本是完全重构的版本,解决了之前版本中的所有已知问题,特别是MySQL保留字冲突问题。主要特点• 简单易用:简洁的界面,易于安装和使用• 响应式设计:适配各种设备,包括手机和平板• 安全可靠:避免使用MySQL保留字,防止SQL注入• 功能完善:支持分类、主题、回复、用户管理等基本功能• 易于扩展:模块化设计,便于

PHP轻论坛 26
查看详情 PHP轻论坛

Some more text.

立即学习PHP免费学习笔记(深入)”;






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.

立即学习PHP免费学习笔记(深入)”;





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 ?
Note 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.
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号