php关于require和include的区别

include() 或 require() 函数,您可以在服务器执行 PHP 文件之前在该文件中插入一个文件的内容,除了它们处理错误的方式不同之外,这两个函数在其他方面都是相同的.

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

  1. <html> 
  2. <body> 
  3. <?php include("header.php"); ?> 
  4. <h1>welcome to my home page</h1> 
  5. <p>some text</p> 
  6. </body> 
  7. </html> 

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

  1. <?php include("menu.php"); ?> 
  2. <h1>welcome to my home page</h1> 
  3. <p>some text</p> 
  4. </body> 
  5. </html> 

require() 函数

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

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

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

  1. <html> 
  2. <body> 
  3. <?php 
  4. include("wrongfile.php"); 
  5. echo "hello world!"
  6. ?> 
  7. </body> 
  8. </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!
波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » php关于require和include的区别

1 评论

  1. Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Onwin , Onwin Giriş , Onwin Güncel Giriş , Onwin Yeni Adres , onwin

评论已关闭

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡