如何在 PHP 中获取有用的错误消息? [英] How can I get useful error messages in PHP?

查看:25
本文介绍了如何在 PHP 中获取有用的错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常会尝试运行一个 PHP 脚本,然后返回一个空白屏幕.没有错误信息;只是一个空屏幕.原因可能是一个简单的语法错误(错误的括号、缺少分号),或者函数调用失败,或者完全是其他原因.

Quite often I will try and run a PHP script and just get a blank screen back. No error message; just an empty screen. The cause might have been a simple syntax error (wrong bracket, missing semicolon), or a failed function call, or something else entirely.

很难弄清楚哪里出了问题.我最终会注释掉代码,在任何地方输入echo"语句等等,试图缩小问题的范围.但肯定有更好的方法,对吧?

It is very difficult to figure out what went wrong. I end up commenting out code, entering "echo" statements everywhere, etc. trying to narrow down the problem. But there surely must be a better way, right?

有没有办法让 PHP 产生有用的错误消息,就像 Java 那样?

Is there a way to get PHP to produce a useful error message, like Java does?

推荐答案

对于语法错误,需要在php.ini中开启错误显示.默认情况下,这些是关闭的,因为您不希望客户"看到错误消息.检查此页面 PHP 文档中的信息2 个指令:error_reportingdisplay_errors.display_errors 可能是您想要更改的那个.如果无法修改 php.ini,也可以在 .htaccess 文件中添加以下几行:

For syntax errors, you need to enable error display in the php.ini. By default these are turned off because you don't want a "customer" seeing the error messages. Check this page in the PHP documentation for information on the 2 directives: error_reporting and display_errors. display_errors is probably the one you want to change. If you can't modify the php.ini, you can also add the following lines to an .htaccess file:

php_flag  display_errors        on
php_value error_reporting       2039

您可能需要考虑将 E_ALL 的值(如 Gumbo 提到的)用于您的 PHP 版本,以便 error_reporting 获取所有错误.更多信息

You may want to consider using the value of E_ALL (as mentioned by Gumbo) for your version of PHP for error_reporting to get all of the errors. more info

其他 3 项:(1) 您可以检查错误日志文件,因为它将包含所有错误(除非已禁用日志记录).(2) 添加以下2行将帮助您调试不是语法错误的错误:

3 other items: (1) You can check the error log file as it will have all of the errors (unless logging has been disabled). (2) Adding the following 2 lines will help you debug errors that are not syntax errors:

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

(3) 另一种选择是使用编辑器在您键入时检查错误,例如 PhpEd.PhpEd 还带有一个调试器,可以提供更详细的信息.(PhpEd 调试器与 xdebug 非常相似,直接集成到编辑器中,因此您可以使用 1 个程序完成所有工作.)

(3) Another option is to use an editor that checks for errors when you type, such as PhpEd. PhpEd also comes with a debugger which can provide more detailed information. (The PhpEd debugger is very similar to xdebug and integrates directly into the editor so you use 1 program to do everything.)

Cartman 的 link 也很好:http://www.ibm.com/developerworks/library/os-debug/

Cartman's link is also very good: http://www.ibm.com/developerworks/library/os-debug/

这篇关于如何在 PHP 中获取有用的错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆