views:

1505

answers:

10

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 the down the problem. But there surely must be a better way, right?.

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

+2  A: 

IBM Developer Works has some useful tricks over here .

cartman
+12  A: 

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_reporing 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(E_ALL);
ini_set('display_errors', 'On');

(3) Another option is to use an editor that checks for errors why 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/

Darryl Hein
Can it be you just downvoted two people that gave the same advice as you did (see your second code sample)?
Tomalak
Did you read my entire answer? I specifically say this won't work for syntax errors, whereas you don't mention that. Putting your code in would make no difference.
Darryl Hein
That's right. I should have thought of mentioning it.
Tomalak
2039 is the value of `E_ERROR | E_WARNING | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING | E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE`. See http://docs.php.net/manual/en/errorfunc.constants.php
Gumbo
+1  A: 

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');
soulmerge
This doesn't work for syntax errors as Candidasa mentioned.
Darryl Hein
Yes, but that is already covered in all other answers.
soulmerge
+2  A: 

You can include the following lines in the file you want to debug:

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

This overrides the default settings in php.ini, which just make PHP report the errors to the log.

Tomalak
This doesn't work for syntax errors as Candidasa mentioned.
Darryl Hein
That's true. In this case the values must be set in the ini directly -- for a pure development environment this may be preferable anyway.
Tomalak
+2  A: 
error_reporting(E_ALL | E_STRICT);

And turn on display errors in php.ini

Ólafur Waage
A: 

You can enable full error reporting (including notices and strict messages). Some people find this too verbose, but it's worth a try. Set error_reporting to E_ALL | E_STRICT in your php.ini.

error_reporting = E_ALL | E_STRICT

E_STRICT will notify you about deprecated functions and give you recommendations about the best methods to do certain tasks.

If you don't want notices, but you find other message types helpful, try excluding notices:

error_reporting = (E_ALL | E_STRICT) & ~E_NOTICE

Also make sure that display_errors is enabled in php.ini. If your PHP version is older than 5.2.4, set it to On:

display_errors = "On"

If your version is 5.2.4 or newer, use:

display_errors = "stderr"
Ayman Hourieh
+8  A: 

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

gnarf
Indeed, this is a *very* useful debugging tool—makes error messages much more verbose, with full stack traces and variable dumps and everything.
htw
Yes. And then use something like the VimDebugger plugin to step through your code and find out where it goes wrong.
Sander Marechal
+1 I use eclipse with xdebug for full step over/step into debugging. Makes PHP development sane!
Wayne
NetBeans with xdebug here. It's so awesome. I'm new to PHP (usually ASP.NET) and had been issuing echo statements before.
Some Canuck
+2  A: 

FirePHP can be useful as well.

Rich Bradshaw
A: 

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.

Daniel S
As with Tomalak's answer, this doesn't work for syntax errors.
Darryl Hein
A: 

Aside from error_reporting and the display_errors ini setting, you can get SYNTAX errors from your web server's log files. When I'm developing PHP I load my development system's web server logs into my editor. Whenever I test a page and get a blank screen, the log file goes stale and my editor asks if I want to reload it. When I do, I jump to the bottom and there is the syntax error. For example:

[Sun Apr 19 19:09:11 2009] [error] [client 127.0.0.1] PHP Parse error:  syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in D:\\webroot\\test\\test.php on line 9
jmucchiello