如何显示 PHP 错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1053424/
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
How do I get PHP errors to display?
提问by Abs
I have checked my PHP ini file (php.ini) and display_errorsis set and also error reporting is E_ALL. I have restarted my Apache webserver.
我检查了我的 PHP ini 文件 ( php.ini) 并display_errors设置了错误报告也是E_ALL. 我已经重新启动了我的 Apache 网络服务器。
I have even put these lines at the top of my script, and it doesn't even catch simple parse errors. For example, I declare variables with a "$"and I don't close statements";". But all my scripts show a blank page on these errors, but I want to actually see the errorsin my browser output.
我什至将这些行放在脚本的顶部,它甚至没有捕获简单的解析错误。例如,我用 a 声明变量"$"并且我不关闭 statements ";"。但是我的所有脚本都在这些错误上显示了一个空白页面,但我想实际查看浏览器输出中的错误。
error_reporting(E_ALL);
ini_set('display_errors', 1);
What is left to do?
还剩下什么?
回答by Fancy John
This always works for me:
这总是对我有用:
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
However, this doesn't make PHP to show parse errors - the only way to show those errors is to modify your php.ini with this line:
但是,这不会使 PHP 显示解析错误 - 显示这些错误的唯一方法是使用以下行修改您的 php.ini :
display_errors = on
(if you don't have access to php.ini, then putting this line in .htaccessmight work too):
(如果您无权访问php.ini,则输入此行.htaccess也可能有效):
php_flag display_errors 1
回答by Michael Madsen
You can't catch parse errors when enabling error output at runtime, because it parses the file before actually executing anything (and since it encounters an error during this, it won't execute anything). You'll need to change the actual server configuration so that display_errors is on and the approriate error_reporting level is used. If you don't have access to php.ini, you may be able to use .htaccess or similar, depending on the server.
在运行时启用错误输出时,您无法捕获解析错误,因为它会在实际执行任何内容之前解析文件(并且由于在此期间遇到错误,它不会执行任何内容)。您需要更改实际的服务器配置,以便启用 display_errors 并使用适当的 error_reporting 级别。如果您无权访问 php.ini,您可以使用 .htaccess 或类似文件,具体取决于服务器。
This questionmay provide additional info.
这个问题可能会提供额外的信息。
回答by user1803477
Inside your php.ini:
在你的php.ini 中:
display_errors = on
Then restart your web server.
然后重新启动您的 Web 服务器。
回答by andre
To display all errors you need to:
要显示所有错误,您需要:
1. Have these lines in the PHP script you're calling from the browser (typically index.php):
1. 在您从浏览器调用的 PHP 脚本中包含这些行(通常为index.php):
error_reporting(E_ALL);
ini_set('display_errors', '1');
2.(a) Make sure that this script has no syntax errors
2.(a) 确保这个脚本没有语法错误
—or—
-或者-
2.(b) Set display_errors = Onin your php.ini
2.(b) 设置display_errors = On在你的php.ini
Otherwise, it can't even run those 2 lines!
否则,它甚至无法运行那 2 行!
You can check for syntax errors in your script by running (at the command line):
您可以通过运行(在命令行中)来检查脚本中的语法错误:
php -l index.php
If you includethe script from another PHP script then it willdisplay syntax errors in the includedscript. For example:
如果您包括从另一个PHP脚本的脚本,那么它就会显示在语法错误包括脚本。例如:
index.php
index.php
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Any syntax errors here will result in a blank screen in the browser
include 'my_script.php';
my_script.php
my_script.php
adjfkj // This syntax error will be displayed in the browser
回答by Kalhua
Some web hosting providers allow you to change PHP parameters in the .htaccessfile.
某些 Web 托管服务提供商允许您更改.htaccess文件中的PHP 参数。
You can add the following line:
您可以添加以下行:
php_value display_errors 1
I had the same issue as yours and this solution fixed it.
我和你有同样的问题,这个解决方案解决了它。
回答by Frank Forte
You might find all of the settings for "error reporting" or "display errors" do not appear to work in PHP 7. That is because error handling has changed. Try this instead:
您可能会发现“错误报告”或“显示错误”的所有设置在 PHP 7 中似乎都不起作用。这是因为错误处理已更改。试试这个:
try{
// Your code
}
catch(Error $e) {
$trace = $e->getTrace();
echo $e->getMessage().' in '.$e->getFile().' on line '.$e->getLine().' called from '.$trace[0]['file'].' on line '.$trace[0]['line'];
}
Or, to catch exceptions and errors in one go (this is not backward compatible with PHP 5):
或者,一次性捕获异常和错误(这与 PHP 5 不向后兼容):
try{
// Your code
}
catch(Throwable $e) {
$trace = $e->getTrace();
echo $e->getMessage().' in '.$e->getFile().' on line '.$e->getLine().' called from '.$trace[0]['file'].' on line '.$trace[0]['line'];
}
回答by Abhijit Jagtap
Use:
用:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
This is the best way to write it, but a syntax error gives blank output, so use the console to check for syntax errors. The best way to debug PHP code is to use the console; run the following:
这是最好的编写方式,但是语法错误会给出空白输出,因此请使用控制台检查语法错误。调试 PHP 代码的最佳方式是使用控制台;运行以下命令:
php -l phpfilename.php
回答by Lead Developer
This will work:
这将起作用:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
回答by Sumit Gupta
Set this in your index.phpfile:
在你的index.php文件中设置:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
回答by NavyaKumar
Create a file called php.iniin the folder where your PHP file resides.
在 PHP 文件所在的文件夹中创建一个名为php.ini的文件。
Inside php.ini add the following code (I am giving an simple error showing code):
在 php.ini 中添加以下代码(我给出了一个显示代码的简单错误):
display_errors = on
display_startup_errors = on

