php 显示所有错误和警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5438060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Showing all errors and warnings
提问by oshirowanen
UPDATE 2:
更新 2:
I have now removed the following from the .php file:
我现在已经从 .php 文件中删除了以下内容:
<?php error_reporting( E_ALL ); ?>
I have set display_erros in php.ini as follows:
我在 php.ini 中设置了 display_erros 如下:
display_errors = On
display_errors = 开
Error reporting is set to the following in php.ini
:
错误报告设置为以下内容php.ini
:
error_reporting = E_ALL | E_STRICT
error_reporting = E_ALL | E_STRICT
After restarting Apache, I still get no errors/warnings.
重新启动 Apache 后,我仍然没有收到错误/警告。
UPDATE 1:
更新1:
I have changed error_reporting in php.ini
from:
我已将 error_reporting 更改php.ini
为:
error_reporting = E_ALL & ~E_DEPRECATED
error_reporting = E_ALL & ~E_DEPRECATED
to
到
error_reporting = E_ALL | E_STRICT
error_reporting = E_ALL | E_STRICT
After which I restarted Apache, e.g.
之后我重新启动了Apache,例如
/etc/init.d/apache2 restart
/etc/init.d/apache2重启
But the page will still not display errors/warnings of any kind.
但是页面仍然不会显示任何类型的错误/警告。
ORIGINAL QUESTION:
原问题:
The following script is generating a warning because the $err being inside the if statement. Why is this warning not being displayed on the PHP page in a web browser?
以下脚本生成警告,因为 $err 位于 if 语句中。为什么此警告未显示在 Web 浏览器的 PHP 页面上?
I have to look at the Apache logs to see the warning. Also, if I deliberately change the "insert into" to "delete into", it does not display an error on the PHP page. Why are the errors not displaying on the actual PHP page?
我必须查看 Apache 日志才能看到警告。另外,如果我故意将“插入”更改为“删除”,则不会在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>
回答by txyoji
Display errors could be turned off in the php.ini
or your Apache configuration file.
可以在php.ini
Apache 配置文件中关闭显示错误。
You can turn it on in the script:
您可以在脚本中打开它:
error_reporting(E_ALL);
ini_set('display_errors', 1);
You should see the same messages in the PHP error log.
您应该会在 PHP 错误日志中看到相同的消息。
回答by Rajesh Patel
I was able to get all errors via the below code:
我能够通过以下代码获得所有错误:
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
回答by John Magnolia
Straight from the php.ini file:
直接来自 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
For pure development I go for:
对于纯粹的开发,我选择:
error_reporting = E_ALL ^ E_NOTICE ^ E_WARNING
Also don't forget to put display_errors to on
也不要忘记把 display_errors 放在
display_errors = On
After that, restart your server for Apache on Ubuntu:
之后,在 Ubuntu 上为 Apache 重新启动服务器:
sudo /etc/init.d/apache2 restart
回答by vkp
PHP errors can be displayed by any of below methods:
PHP 错误可以通过以下任何一种方法显示:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
For more details:
更多细节:
回答by rinjan
Set these on php.ini
:
设置这些php.ini
:
;display_startup_errors = On
display_startup_errors=off
display_errors =on
html_errors= on
From your PHP page, use a suitable filter for error reporting.
在您的 PHP 页面中,使用合适的过滤器来报告错误。
error_reporting(E_ALL);
Filers can be made according to requirements.
可根据要求制作过滤器。
E_ALL
E_ALL | E_STRICT
回答by Suresh Maurya
You can see a detailed description here.
您可以在此处查看详细说明。
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);
Changelog
变更日志
5.4.0 E_STRICT became part of E_ALL
5.3.0 E_DEPRECATED and E_USER_DEPRECATED introduced.
5.2.0 E_RECOVERABLE_ERROR introduced.
5.0.0 E_STRICT introduced (not part of 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 的一部分)。