为什么在php中include()和require()失败时会出现两个错误报告


why two error reports when include() and require() failed in php

我有一个这样的脚本:

<html>
<body>
<?php
require("wrongFile.php");
echo "Hello World!";
?>
</body>
</html>

现在,如果错误文件不存在,下面两个错误将报告:

警告:require(wrongFile.php) [function。在C:'home'website'test.php中没有这样的文件或目录

致命错误:require() [function。]在C:'home'website'test.php中打开错误文件" wrongFile.php " (include_path='.;C:'php5'pear')失败

我知道如果require()函数中不存在文件,则显示致命错误,导致脚本执行停止,但不知道第一个错误是什么

使用include()函数时的第一个错误显示

这很明显,不是吗?: -)

require()函数试图打开您指定的文件时,从它内部抛出第一个警告。它不能打开用于读取文件的流,因为文件不存在。

因此,函数调用本身也失败,这解释了'Fatal error'。

第一个是警告,不是错误。您可以尝试使用@include来抑制它。(当然,这对require没有多大用处)。