【问题标题】:The error thrown by require not visiblerequire 抛出的错误不可见
【发布时间】:2012-02-10 09:49:10
【问题描述】:

我正在阅读 php here 中 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>

【问题讨论】:

    标签: php include require


    【解决方案1】:

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

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

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

    那里有一些实际的输出,你会看到只有'before require'文本被输出-当require()失败时,脚本将终止执行。

    使用您的版本,您没有可见的输出,必须查看浏览器中的页面源才能看到没有&lt;/body&gt;&lt;/html&gt;

    【讨论】:

      【解决方案2】:

      可能display_errors 已禁用。您可以致电phpinfo() 进行检查。

      尝试放置

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

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

      编辑 哇!我同意 Damien 的回答。

      【讨论】:

        【解决方案3】:

        您的测试代码错误,$value 永远不会大于 10。试试这个,您将遇到 致命错误

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

        【讨论】:

        • 谢谢。我做到了,我看到 require 准时给了我一个错误,而 include 给了我 10 次错误。他们之间的区别现在越来越混乱。include 不应该给出错误。
        • Include 会给你一个警告,但不要停止执行你的页面。
        猜你喜欢
        • 1970-01-01
        • 2021-12-02
        • 1970-01-01
        • 1970-01-01
        • 2015-04-28
        • 2012-10-23
        • 1970-01-01
        • 1970-01-01
        • 2020-04-19
        相关资源
        最近更新 更多