教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php关于require和include的区别

php关于require和include的区别

发布时间:2017-10-23   编辑:jiaochengji.com
教程集为您提供php关于require和include的区别等资源,欢迎您收藏本站,我们将为您提供最新的php关于require和include的区别资源

include() 或 require() 函数,您可以在服务器执行 php教程 文件之前在该文件中插入一个文件的内容。除了它们处理错误的方式不同之外,这两个函数在其他方面都是相同的。include() 函数会生成一个警告(但是脚本会继续执行),而 require() 函数会生成一个致命错误(fatal error)(在错误发生后脚本会停止执行)。

<blockquote>

<html>
<body>

<?php include("header.php"); ?>

<h1>welcome to my home page</h1>

<p>some text</p>

</body>
</html>

</blockquote>

三个文件,"default.php"、"about.php" 以及 "contact.php" 都引用了 "menu.php" 文件。这是 "default.php" 中的代码:

<blockquote>

<?php include("menu.php"); ?>

<h1>welcome to my home page</h1>

<p>some text</p>

</body>
</html>


 

</blockquote>

require() 函数
require() 函数与 include() 相同,不同的是它对错误的处理方式。

include() 函数会生成一个警告(但是脚本会继续执行),而 require() 函数会生成一个致命错误(fatal error)(在错误发生后脚本会停止执行)。

如果在您通过 include() 引用文件时发生了错误,会得到类似下面这样的错误消息:

php 代码:

<blockquote>

<html>
<body>

<?php
include("wrongfile.php");
echo "hello world!";
?>

</body>
</html>错误消息:
warning: include(wrongfile.php) [function.include]:
failed to open stream:
no such file or directory in c:homewebsitetest.php on line 5

warning: include() [function.include]:
failed opening 'wrongfile.php' for inclusion
(include_path='.;c:php5pear')
in c:homewebsitetest.php on line 5

hello world!

</blockquote>

您可能感兴趣的文章:
谈谈PHP中require和include的区别
include,require,以及后缀加once的区别
php中require和include的区别是什么?
php的use和require的区别
php require和include的区别
php中的include,require,include_once,require_once
php use和include区别
php关于require和include的区别
PHP文件包含语句 include、include_once、require、require_onc
php引入文件的方法有哪些

[关闭]
~ ~