require引发的错误不可见


The error thrown by require not visible

我在php中读到了include和require之间的区别。

require will throw a PHP Fatal Error if the file cannot be loaded. 

我在php中创建了一个测试文件,以更好地了解差异,但它们都没有显示任何内容(我在require中没有看到任何错误)。

请帮帮我。感谢

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
for( $value = 0; $value < 10; $value++ )
if($value>10)
require("boom.php"); // no such file exits in real
?>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    <body>
    <?php
    for( $value = 0; $value < 10; $value++ )
    if($value>10)
    include("boom.php"); // no such file exits in real
    ?>
    </body>
    </html>

您的测试代码是错误的,$value永远不会大于10。尝试此操作,您将出现致命错误:

<?php
require("boom.php"); // no such file exits in real
?>

可能您的PHP关闭了display_errors,这意味着您将不会在客户端输出中看到错误消息。您应该在开发环境的php.ini中启用此设置。

如果你有这样的东西,你至少会看到失败:

<html>
<body>
<p>Before require</p>
<?php require('does-not-exist'); ?>
<p>After require</p>
</body>
</html>

有了一些实际的输出,您会看到只有"before-request"文本被输出——当require()失败时,脚本将终止执行。

在您的版本中,您没有可见的输出,必须查看浏览器中的页面源代码才能看到没有</body></html>

可能display_errors已禁用。您可以通过调用phpinfo()进行检查。

尝试放置

ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);

在脚本的开头,直接在屏幕上显示错误(仅推荐用于开发系统)。

编辑Doh!我同意达米恩的回答。