显示所有错误和警告 [重复]

Showing all errors and warnings [duplicate]

提问人:oshirowanen 提问时间:3/26/2011 最后编辑:Peter Mortensenoshirowanen 更新时间:9/17/2022 访问量:1331618

问:

更新2:

我现在已从 .php 文件中删除以下内容:

<?php error_reporting( E_ALL ); ?>

我在php中设置了display_erros.ini如下所示:

display_errors = 开

错误报告设置为以下值:php.ini

error_reporting = E_ALL |E_STRICT

重新启动 Apache 后,我仍然没有收到任何错误/警告。

更新1:

我已将error_reporting从:php.ini

error_reporting = E_ALL & ~E_DEPRECATED

error_reporting = E_ALL |E_STRICT

之后我重新启动了Apache,例如

/etc/init.d/apache2 重新启动

但该页面仍然不会显示任何类型的错误/警告。

原问题:

以下脚本正在生成警告,因为$err位于 if 语句中。为什么在 Web 浏览器的 PHP 页面上没有显示此警告?

我必须查看Apache日志才能看到警告。另外,如果我故意将“insert into”更改为“delete into”,它不会在PHP页面上显示错误。为什么错误没有显示在实际的PHP页面上?

<?php
    error_reporting(E_ALL);
?>

<html>
    <head>
        <title></title>
        <link rel="icon" type="image/png" href="favicon.ico">

        <?php
            if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                $err = array();

                if (empty( $_POST['display_name']))
                    $err[] = "display name field is required";
                if (empty( $_POST['email']))
                    $err[] = "email field is required";
                if (empty( $_POST['password']))
                    $err[] = "password field is required";

                if (!$err) {
                    try {
                        $DBH = new PDO("mysql:host=localhost;dbname=database1", "user", "pass");
                        $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                        $STH = $DBH->prepare("delete into table1 (display_name, email, password) values ( :display_name, :email, :password )");

                        $STH->bindParam(':display_name', $_POST['display_name'], PDO::PARAM_STR, 100);
                        $STH->bindParam(':email', $_POST['email'], PDO::PARAM_STR, 100);
                        $STH->bindParam(':password', $_POST['password'], PDO::PARAM_STR, 100);

                        $STH->execute();

                        $STH = $DBH->prepare("delete into table2 ( username, status, users_id ) values ( :username, :status, :users_id )");

                        $strStatus = 1;

                        $STH->bindParam(':username', $_POST['display_name'], PDO::PARAM_STR, 100);
                        $STH->bindParam(':status', $strStatus, PDO::PARAM_INT, 1);
                        $STH->bindParam(':users_id', $_POST['referer'], PDO::PARAM_INT, 1);

                        $STH->execute();

                        $DBH = null;
                    }
                    catch (PDOException $e) {
                        echo $e->getMessage();
                    }

                    header("Location: " . $_SERVER['PHP_SELF']);
                    exit;
                }
                else {
                    foreach ($_POST as $key => $val) {
                        $form[$key] = htmlspecialchars($val);
                    }
                }
            }
            else {
                $form['display_name'] = $form['email'] = $form['password'] = '';
            }
        ?>
    </head>

    <body>
        <?php foreach($err as $line) { ?>
        <div style="error"><?php echo $line; ?></div>
        <?php } ?>

        <h1>Register</h1>

        <form method="post">
            Referers id:<br/>
            <input type="text" name="referer" /><br/><br/>

            Name:<br/>
            <input type="text" name="display_name" value="<?php echo $form['display_name']; ?>" /><br/><br/>

            Email:<br/>
            <input type="text" name="email" value="<?php echo $form['email']; ?>" /><br/><br/>

            Password:<br/>
            <input type="text" name="password" value="<?php echo $form['password']; ?>" /><br/><br/>

            <input type="submit" value="register" />
        </form>
    </body>
</html>
PHP 调试 警告

评论

1赞 salathe 3/26/2011
display_errors INI 设置的当前值是多少?
0赞 oshirowanen 3/26/2011
我刚刚检查了一下,它被设置为 error_reporting = E_ALL & ~E_DEPRECATED
0赞 salathe 3/26/2011
请重新阅读我的问题,我们已经知道您的设置,因为它在脚本中。error_reporting
0赞 oshirowanen 3/26/2011
请参阅原始问题中的更新 1:
2赞 salathe 3/26/2011
请重新阅读我的问题。 与 完全不同。此外,脚本将覆盖 INI 文件中设置的值。display_errorserror_reportingerror_reporting

答:

1303赞 5 revs, 4 users 62%txyoji #1

可以在 Apache 配置文件中关闭显示错误。php.ini

您可以在脚本中打开它:

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

您应该在 PHP 错误日志中看到相同的消息。

评论

2赞 oshirowanen 3/26/2011
更改 php.ini 文件后需要重新启动 apache 吗?
1赞 Elangovan 12/3/2014
这个问题已经发布 stackoverflow.com/questions/1053424/...
4赞 jave.web 12/9/2014
@oshirowanen实际上并没有更改文件,脚本只是像在...从手动:在脚本执行期间将在脚本结束时恢复ini_set()php.iniphp.iniSets the value of the given configuration option. The configuration option will keep this new value , and .
5赞 Ravi Dhoriya ツ 3/12/2015
我不知道有多少次我得到了同样的答案。你帮了我很多。谢谢!:)
1赞 Nick Rolando 7/22/2016
@Charles 为什么要删除?E_ALL不是包括一切,包括E_NOTICE吗?E_ALLE_NOTICE
42赞 John Magnolia 11/16/2012 #2

直接从 php.ini 文件:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Error handling and logging ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; This directive informs PHP of which errors, warnings and notices you would like
; it to take action for. The recommended way of setting values for this
; directive is through the use of the error level constants and bitwise
; operators. The error level constants are below here for convenience as well as
; some common settings and their meanings.
; By default, PHP is set to take action on all errors, notices and warnings EXCEPT
; those related to E_NOTICE and E_STRICT, which together cover best practices and
; recommended coding standards in PHP. For performance reasons, this is the
; recommend error reporting setting. Your production server shouldn't be wasting
; resources complaining about best practices and coding standards. That's what
; development servers and development settings are for.
; Note: The php.ini-development file has this setting as E_ALL. This
; means it pretty much reports everything which is exactly what you want during
; development and early testing.
;
; Error Level Constants:
; E_ALL             - All errors and warnings (includes E_STRICT as of PHP 5.4.0)
; E_ERROR           - fatal run-time errors
; E_RECOVERABLE_ERROR  - almost fatal run-time errors
; E_WARNING         - run-time warnings (non-fatal errors)
; E_PARSE           - compile-time parse errors
; E_NOTICE          - run-time notices (these are warnings which often result
;                     from a bug in your code, but it's possible that it was
;                     intentional (e.g., using an uninitialized variable and
;                     relying on the fact it is automatically initialized to an
;                     empty string)
; E_STRICT          - run-time notices, enable to have PHP suggest changes
;                     to your code which will ensure the best interoperability
;                     and forward compatibility of your code
; E_CORE_ERROR      - fatal errors that occur during PHP's initial startup
; E_CORE_WARNING    - warnings (non-fatal errors) that occur during PHP's
;                     initial startup
; E_COMPILE_ERROR   - fatal compile-time errors
; E_COMPILE_WARNING - compile-time warnings (non-fatal errors)
; E_USER_ERROR      - user-generated error message
; E_USER_WARNING    - user-generated warning message
; E_USER_NOTICE     - user-generated notice message
; E_DEPRECATED      - warn about code that will not work in future versions
;                     of PHP
; E_USER_DEPRECATED - user-generated deprecation warnings
;
; Common Values:
;   E_ALL (Show all errors, warnings and notices including coding standards.)
;   E_ALL & ~E_NOTICE  (Show all errors, except for notices)
;   E_ALL & ~E_NOTICE & ~E_STRICT  (Show all errors, except for notices and coding standards warnings.)
;   E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR  (Show only errors)
; Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
; Development Value: E_ALL
; Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT
; http://php.net/error-reporting
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT

对于纯粹的开发,我选择:

    error_reporting = E_ALL ^ E_NOTICE ^ E_WARNING

另外,别忘了穿上display_errors

display_errors = On

之后,在 Ubuntu 上重新启动 Apache 服务器:

sudo /etc/init.d/apache2 restart

评论

7赞 Bell 10/29/2016
“PHP 6.0.0”?呵呵:-)
1赞 Lucas 4/5/2019
“对于纯粹的发展,@JohnMagnolia去追求:。”符号是否连接了这三个选项?如果是这样,是否已经包括 和 ?error_reporting = E_ALL ^ E_NOTICE ^ E_WARNING^E_ALLE_NOTICEE_WARNING
11赞 rinjan 10/31/2015 #3

设置这些:php.ini

;display_startup_errors = On
display_startup_errors=off
display_errors =on
html_errors= on

在 PHP 页面中,使用合适的过滤器进行错误报告。

error_reporting(E_ALL);

可根据要求制作文件。

E_ALL
E_ALL | E_STRICT
69赞 Rajesh Patel 1/14/2016 #4

我能够通过以下代码获得所有错误:

ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);

评论

0赞 John Contarino 7/12/2019
在我的服务器上唯一对我有用的代码 (IONOS)
1赞 Jsowa 5/2/2020
error_reporting(-1);不应使用。它可能不适用于每个系统。相反,应该是error_reporting(E_ALL);
0赞 Herbert Van-Vliet 5/16/2020
error_reporting(-1) 应该可以正常工作,因为它在这里作为示例给出: php.net/manual/en/function.error-reporting.php
1赞 ashleedawg 9/26/2020
的值实际上应该是字符串,例如根据文档,这在使用严格类型时是强制执行的(例如,ini_setini_set('display_errors', '1');declare(strict_types=1);)
46赞 vkp 2/10/2017 #5

PHP 错误可以通过以下任何一种方法显示:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

更多详情:

显示 PHP 错误

评论

6赞 Nabi K.A.Z. 7/23/2017
PHP 文件中的 .display_errors = on;syntax error
7赞 imskm 5/12/2018 #6

您可以在此处查看详细说明。

ini_set('display_errors', 1);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);

// Report all PHP errors (see changelog)
error_reporting(E_ALL);

// Report all PHP errors
error_reporting(-1);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

更改日志

  • 5.4.0 E_STRICT成为E_ALL的一部分

  • 5.3.0 引入了E_DEPRECATED和E_USER_DEPRECATED。

  • 5.2.0 E_RECOVERABLE_ERROR引入。

  • 5.0.0 E_STRICT引入(不是 E_ALL 的一部分)。