php中的include和require的区别 - 新闻中心 - 长春市隆兴伟业物流有限公司
现在的位置: 主页 > 新闻中心 > 文章正文
php中的include和require的区别
作者:长春市隆兴伟业物流有限公司 来源:www.lxwywl.com 发布时间:2017-09-07 13:22:16
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:

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":

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:

Then, if we include the "vars.php" file, the variables can be used in the calling file:

Example


Welcome to my home page!




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!




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!




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.

,专题系统

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:湖北网站建设 http://hubei.45qun.com


  • 上一篇:Android 注解的一些应用以及原理
  • 下一篇:最后一页
  • 
    COPYRIGHT © 2015 长春市隆兴伟业物流有限公司 ALL RIGHTS RESERVED.
    本站所有原创信息,未经许可请勿任意转载或复制使用 网站地图 技术支持:肥猫科技
    精彩专题:网站建设
    购买本站友情链接、项目合作请联系客服QQ:2500-38-100