Home  >  Article  >  Backend Development  >  PHP中的include跟require

PHP中的include跟require

WBOY
WBOYOriginal
2016-06-13 12:23:20746browse

PHP中的include和require

一、有什么用?

服务器端包含 (Server Side Include,SSI) 用于创建可在多个页面重复使用的函数、页眉、页脚或元素。包含文件重用了文件,省去了许多工作。

二、怎么用?

include ‘filename’;

require ‘filename’;


1.php

<code class=" hljs php"><span class="hljs-preprocessor"><?php</span><span class="hljs-keyword">echo</span> <span class="hljs-string">"Hello "</span>;<span class="hljs-keyword">include</span> <span class="hljs-string">'2.php'</span>;<span class="hljs-preprocessor">?></span></code>

2.php

<code class=" hljs xml"><span class="php"><span class="hljs-preprocessor"><?php</span><span class="hljs-keyword">echo</span> <span class="hljs-string">"World."</span>;<span class="hljs-preprocessor">?></span></span></code>

在浏览器访问1.php会得到(注意此时1.php和2.php是放在同一个文件夹里的):Hello World.

三、include跟require有什么区别?

在错误处理方面:
require 会生成致命错误(E_COMPILE_ERROR)并停止脚本
include 只生成警告(E_WARNING),并且脚本会继续


请在此时使用:
请在此时使用 require:当文件被应用程序请求时。
请在此时使用 include:当文件不是必需的,且应用程序在文件未找到时应该继续运行时。

版权声明:本文为Lshare原创文章,需要转载的请联系我,有问题欢迎评论或私信。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn