How do I effectively debug PHP applications? My current method is to use trace output - eg: die("message") and then fix the application from there. The problem is that this is often very cumbersome and inefficient. I would like to do something like this:
define('DEBUG', true);
debug({ print_r($arr); });
on_debug_die("application exit");
But I know that PHP doesn't support that kind of syntax. Can someone help me out?
9 Answers 9
Use http://xdebug.org/ and a proper IDE. It changes the dev game :)
Comments
You can have it like this:
define('DEBUG', "DEBUG"); //Comment this line when you are done debugging
function debug_out($input) {
if (defined("DEBUG")) {
print_r($input);
die;
}
}
Now, whenever you need debugging, call debug_out('...');.
The better way to do this, of course, is using debugging facilities like XDebug with IDE's that support it, like PhpStorm.
Comments
Having debug code intermingled within your application can seriously damage your code readability and performance. The best approach I can advise you to take, is to start thinking about unit tests (google) and why not - your ad hoc code debuging with die statements - that one never grows old. Integrated IDE debugging is also an option (PhpEd, Net Beans, etc. they all feature such fancy stuff).
Comments
Check out a logging framework like log4php - then you can just add statements like the below:
$log->debug($arr);
and modify a central log configuration to control what level (debug, info, warn, error, et cetera) of log messages are actually printed.
Comments
why not using Eclipse PDT & Xdebug ? or PHPStorm (another good IDE but which is paying)
Using eclipse PDT & XDebug or Zend Debugger engine, you'll be able to do step debugging, inspect variables & put breakpoints in your PHP Code (even remotely)
This is much more "modern" way to debug app than tracing. tracing is necessary but definitely not efficient for day to day dev.
Comments
Your use of debug() can easily be implemented, unless I misunderstand it: just pass $arr as an argument and print_r() it within the function. Can you explain the behaviour of on_debug_die()?
Comments
how so?
function on_debug_die($msg) {
if (defined('DEBUG')) die($msg);
}
on_debug_die(print_r($arr,1));
there can be a problem with var_dump() function which is more useful for deugging purposes but it is solvable too.
however, using die() will stop your program execution on the first one. So, it seems that using trigger_error() instead of die() looks like more useful
Comments
You could always hook it up to a real debugger using, for example, xdebug.
Or you can define a function such as:
function debug() {
if (!DEBUG) {
return;
}
$args = get_func_args();
// var_dump, log to file, whatever
}
Or you can structure your application well from the beginning using type hints, objects and throwing exceptions, which makes errors mostly report themselves.
Comments
For this specific use you can employ this function:
function debug($val) {
if (defined('DEBUG') && DEBUG) {
echo '<pre>', var_dump($val), '</pre>';
die();
};
};
This will check if the DEBUG constant is defined and evaluated to true, and if yes, then will effectively stop script execution, outputting $val just before that. You can also think about logging some values or important steps into some files - many PHP frameworks implement similar features.
Is this what you wanted? More on debugging you can read here: http://php.net/manual/en/debugger.php