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

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

问题描述

我发现PHP中的编程非常令人沮丧。很多时候我会尝试运行脚本,只是回到空白屏幕。没有错误信息,只是空屏幕。原因可能是一个简单的语法错误(错误的括号,缺少分号)或失败的函数调用,或其他完整的。



很难弄清楚什么地方出了错。我最终注释掉代码,输入echo语句到处等等,试图缩小问题。但是,肯定一定是一个更好的方法,对吗?



那么,有没有办法让PHP生成有用的错误消息,如Java?
任何人都可以推荐好的PHP调试技巧,工具和技巧?

解决方案

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

  php_flag display_errors在
php_value error_reporting 2039

您可能需要考虑使用E_ALL的值(如上所述通过Gumbo)为您的版本的PHP $ error_reporting 获得所有的错误。 更多信息



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

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

(3)另一个选项是使用编辑器,当您输入时检查错误,例如<一个href =http://www.nusphere.com/products/phped.htm =noreferrer> PhpEd 。 PhpEd还附带一个可以提供更详细信息的调试器。 (PhpEd调试器非常类似于xdebug,并直接集成到编辑器中,因此您可以使用1个程序来执行所有操作。)



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


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

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?.

So, is there a way to get PHP to produce useful error message like Java does? Can anyone recommend good PHP debugging tips, tools and techniques?

解决方案

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

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 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) 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's link is also very good: http://www.ibm.com/developerworks/library/os-debug/

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

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