How to get useful error messages in PHP?

2019-01-29 02:47发布

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?

29条回答
【Aperson】
2楼-- · 2019-01-29 03:30

http://todell.com/debug can be useful as well. You can see your object values or thrown debug errors behind the scene even in production mode.

查看更多
闹够了就滚
3楼-- · 2019-01-29 03:31

To turn on full error reporting, add this to your script:

error_reporting(E_ALL);

This causes even minimal warnings to show up. And, just in case:

ini_set('display_errors', '1');

Will force the display of errors. This should be turned off in production servers, but not when you're developing.

查看更多
We Are One
4楼-- · 2019-01-29 03:33

There is a really useful extension called "xdebug" that will make your reports much nicer as well.

查看更多
Ridiculous、
5楼-- · 2019-01-29 03:33

In addition to the very many excellent answers above you could also implement the following two functions in your projects. They will catch every non-syntax error before application/script exit. Inside the functions you can do a backtrace and log or render a pleasant 'Site is under maintenance' message to the public.

Fatal Errors:

register_shutdown_function

http://php.net/manual/en/function.register-shutdown-function.php

Errors:

set_error_handler

http://php.net/manual/en/function.set-error-handler.php

Backtracing:

debug_backtrace

http://php.net/manual/en/function.debug-backtrace.php

查看更多
ら.Afraid
6楼-- · 2019-01-29 03:34

The “ERRORS” are the most useful things for the developers to know their mistakes and resolved them to make the system working perfect.

PHP provides some of better ways to know the developers why and where their piece of code is getting the errors, so by knowing those errors developers can make their code better in many ways.

Best ways to write following two lines on the top of script to get all errors messages:

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

Another way to use debugger tools like xdebug in your IDE.

查看更多
姐就是有狂的资本
7楼-- · 2019-01-29 03:35

You can register your own error handler in PHP. Dumping all errors to a file might help you in these obscure cases, for example. Note that your function will get called, no matter what your current error_reporting is set to. Very basic example:

function dump_error_to_file($errno, $errstr) {
    file_put_contents('/tmp/php-errors', date('Y-m-d H:i:s - ') . $errstr, FILE_APPEND);
}
set_error_handler('dump_error_to_file');
查看更多
登录 后发表回答