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?
Try this PHP error reporting reference tool. It's a very good visual reference and helped me understand the complex error reporting mechanism.
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
anddisplay_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: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 info3 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:
(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/
The following enables all errors:
Also see the following links
If you are super cool, you might try:
This will only display errors when you are running locally. It also gives you the test_server variable to use in other places where appropriate.
Any errors that happen before the script runs won't be caught, but for 99% of errors that I make, that's not an issue.
The two key lines you need to get useful errors out of PHP are:
As pointed out by other contributors, these are switched off by default for security reasons. As a useful tip - when you're setting up your site it's handy to do a switch for your different environments so that these errors are ON by default in your local and development environments. This can be achieved with the following code (ideally in your index.php or config file so this is active from the start):
FirePHP can be useful as well.