A quick guide to debugging PHP
Every time there's a long gap between projects, I sometimes forget all the little tricks to debug PHP code. This should serve as kind of a placeholder for my own brain (possibly yours?).
The guide
Required tools
Installation of xdebug
I assume you have PHP installed somewhere, so installation of PHP and Pear won't be covered here.
Pear Method
As root, execute:
pear install xdebug
RPM
manually download my RPM and install it.
From source
Read the
Authoritative guide, but below works if you're impatient.
- Download the source
- Unpack the source, and cd into the top-level folder
- run phpize
- ./configure --enable-xdebug
- cp modules/xdebug.so /path/to/lib/php/modules/
- Add "extension=xdebug.so" somewhere into php.ini
- restart Apache
Modifying your PHP code
This part is pretty simple. We use php's built-in function ini_set() to enable error reporting on a per-page basis (instead of system-wide when modifying it in php.ini). We then set the error level we wish to see using error_reporing().
Example:
$debug = true; // sentinel for all kinds of debugging options
if($debug) {
ini_set( 'display_errors', true );
error_reporting( E_ALL );
}
Now when you run your code, you'll get some very nicely formatted call-back lists to help you trace out where your code failed, and why. Not to mention links to the online PHP manual if you have a syntax error.
Future enhacnements
There's a way to even compile xdebug to have a client hook for gdb. Not sure how, but when I figure it out, I'll post it here.
Optional Reading
Here's a few links on the subject that have helped me in the past..
- Using Gubed with Quanta Plus and PHP
- Another link to using Quanta and Gubed