php关于require和include的区别

前端之家收集整理的这篇文章主要介绍了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 @R_502_126@ 'wrongfile.PHP' for inclusion
(include_path='.;c:PHP5pear')
in c:homewebsitetest.PHP on line 5
 
hello world!

猜你在找的PHP相关文章