简体   繁体   English

php中“include”和“require”的区别

[英]Difference between “include” and “require” in php

Is there any difference between them?它们之间有什么区别吗? Is using them a matter of preference?使用它们是偏好问题吗? Does using one over the other produce any advantages?使用其中一种会产生任何优势吗? Which is better for security?哪个更安全?

require will throw a PHP Fatal Error if the file cannot be loaded.如果无法加载文件, require将抛出 PHP 致命错误。 (Execution stops) (执行停止)

include produces a Warning if the file cannot be loaded.如果无法加载文件, include会产生警告。 (Execution continues) (执行继续)

Here is a nice illustration of include and require difference :这是include 和 require 差异的一个很好的说明

在此处输入图片说明

From: Difference require vs. include php (by Robert; Nov 2012)来自:差异需要与包含 php(罗伯特;2012 年 11 月)

You find the differences explained in the detailed PHP manual on the page of require :您可以在require页面详细 PHP 手册中找到解释的差异:

require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error. requireinclude相同,除了失败时它也会产生致命的E_COMPILE_ERROR级别错误。 In other words, it will halt the script whereas include only emits a warning ( E_WARNING ) which allows the script to continue.换句话说,它将停止脚本,而 include 仅发出警告( E_WARNING ),允许脚本继续。

See @efritz's answer for an example有关示例,请参阅@efritz 的答案

Use include if you don't mind your script continuing without loading the file (in case it doesn't exist etc) and you can (although you shouldn't) live with a Warning error message being displayed.如果您不介意脚本在不加载文件的情况下继续(以防它不存在等)并且您可以(尽管您不应该)忍受显示警告错误消息,请使用include

Using require means your script will halt if it can't load the specified file, and throw a Fatal error.使用require意味着如果无法加载指定的文件,您的脚本将暂停,并引发致命错误。

The difference between include() and require() arises when the file being included cannot be found: include() will release a warning ( E_WARNING ) and the script will continue, whereas require() will release a fatal error ( E_COMPILE_ERROR ) and terminate the script. include()require()之间的区别在于无法找到被包含的文件时: include()将释放警告 ( E_WARNING ) 并且脚本将继续,而require()将释放致命错误 ( E_COMPILE_ERROR ) 并终止脚本。 If the file being included is critical to the rest of the script running correctly then you need to use require() .如果包含的文件对脚本的其余部分正确运行至关重要,那么您需要使用require()

For more details : Difference between Include and Require in PHP有关更多详细信息: PHP 中 Include 和 Require 之间的区别

As others pointed out, the only difference is that require throws a fatal error, and include - a catchable warning.正如其他人指出的那样,唯一的区别是 require 抛出一个致命错误,并包含 - 一个可捕获的警告。 As for which one to use, my advice is to stick to include.至于使用哪个,我的建议是坚持包含。 Why?为什么? because you can catch a warning and produce a meaningful feedback to end users.因为您可以捕获警告并向最终用户提供有意义的反馈。 Consider考虑

  // Example 1.
  // users see a standard php error message or a blank screen
  // depending on your display_errors setting
  require 'not_there'; 


  // Example 2.
  // users see a meaningful error message
  try {
      include 'not_there';
  } catch(Exception $e) {
     echo "something strange happened!";
  }

NB: for example 2 to work you need to install an errors-to-exceptions handler, as described here http://www.php.net/manual/en/class.errorexception.php注意:例如 2 工作你需要安装一个错误到异常处理程序,如这里所述http://www.php.net/manual/en/class.errorexception.php

  function exception_error_handler($errno, $errstr, $errfile, $errline ) {
     throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
  }
  set_error_handler("exception_error_handler");   
<?PHP
echo "Firstline";
include('classes/connection.php');
echo "I will run if include but not on Require";
?>

A very simple Practical example with code.一个非常简单的带有代码的实用示例。 The first echo will be displayed.将显示第一个回声。 No matter you use include or require because its runs before include or required.无论您使用 include 还是 require 都是因为它在 include 或 required 之前运行。

To check the result, In second line of a code intentionally provide the wrong path to the file or make error in file name.为了检查结果,在代码的第二行故意提供错误的文件路径或在文件名中出错。 Thus the second echo to be displayed or not will be totally dependent on whether you use require or include .因此,要显示或不显示的第二个回声将完全取决于您是使用require还是include

If you use require the second echo will not execute but if you use include not matter what error comes you will see the result of second echo too.如果您使用require第二个 echo 将不会执行但如果您使用include无论出现什么错误,您也会看到第二个 echo 的结果。

如果 Include 程序不会终止并在浏览器上显示警告,另一方面 Require 程序将在找不到文件的情况下终止并显示致命错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM