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?
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.
To turn on full error reporting, add this to your script:
This causes even minimal warnings to show up. And, just in case:
Will force the display of errors. This should be turned off in production servers, but not when you're developing.
There is a really useful extension called "xdebug" that will make your reports much nicer as well.
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:
http://php.net/manual/en/function.register-shutdown-function.php
Errors:
http://php.net/manual/en/function.set-error-handler.php
Backtracing:
http://php.net/manual/en/function.debug-backtrace.php
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:
Another way to use debugger tools like xdebug in your IDE.
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: