One of the things I've missed most in learning PHP is a debugger. A debugger is a necessity for productive work in any programming language. You need a debugger to step through code to understand how it works and is being called. You need a debugger to examine variables.
But I'm basically a hobbyist, and at this point don't want to spend the money for a professional ide like Zend Studio or PHPEdit which come with built-in debuggers. PDT, Eclipse specialized for PHP programming, is a free option, but it is also a large piece of software that is overkill for my needs, especially since I work off a memory stick quite frequently and don't need another application that will take several hundred megabytes of space.
I've been using Notepad++ as my PHP editor. It has just about everything I want - syntax highlighting, a good search function, basic brace checking, the ability to show white space marks, conversion between Unix style and Windows style line feeds, the ability to use different character encodings, macros, function folding, and the ability to specialize to a specific language. Still and all, it is an editor. As I worked I would wistfully remember my C++ IDE.
But, Notepad++ has a plugin interface. It can be extended to do all sorts of things. So I started wondering if there was a debugging extension for it.
After much scouring of the internet I found there is one, DBPg, a client interface for XDebug. XDebug is the opensource PHP debugger that, along with the Zend debugger, is one of the most popular debuggers for PHP. XDebug lets you set breakpoints and step through your code, set up watches on the variables you want to view, perform stack traces, and view PHP's handler vars like
$_POST, $_GET, $_COOKIE, $_SESSION. When I found it, and then when I found the plugin for Notepad++ that acts as an interface for it, I knew my debugging problems were over.
Setting up Notepad++ to work with XDebug is fairly easy.
First, install XDebug on your server. I placed it in my php extensions directory. Then I added the following lines to my php.ini file to get XDebug working:
[debug] zend_extension_ts="/wos/php5/ext/php_xdebug-2.0.2.dll" xdebug.remote_enable=1 xdebug.remote_host=127.0.0.1 xdebug.remote_port=9000 xdebug.remote_handler=dbgp xdebug.remote_mode=req xdebug.idekey=default
Note 1: I use zend_extension_ts because I'm running a multithreaded version of PHP. If I were runnng a single threaded version of PHP, I would use zend_extension (without the _ts).
Note 2: /wos/php5/ext/php_xdebug-2.0.2.dll is the path to where XDebug is on my filesystem.
Restart your server if need be to activate the debugger.
Second, prepare Notepad++ for debugging. Place the DBGp plugin in Notepad++'s plugin directory, then open Notepad++. DBGp will be a new item in the Plugins menu. I configured DBGp by telling it refresh both the local and global contexts on every step, then mapped my local files to the server files. Since I'm running a local server for development, I ignored the remote server ip and the IDE KEY, then set both the remote path and the local path the same, in my case
f:\wos\www, since that is where I have Habari, the script I'm currently working with, located.
That's it. Notepad++ is set up. Notepad++ has several other plugins that make life easier. Visit it's homepage to browse through them.
To start debugging, open the file you want to work on in Notepad++, then click on
Debugger in the DBGp menu. The debugger will open at the bottom of your Notepad++ window. Open up your browser and navigate to the page you want to debug. In the address bar type
?XDEBUG_SESSION_START=test at the end of the url, then refresh the page. The Notepad++ icon will flash to let you know it is connected, and you can get to work.
If you're using Firefox as your browser, there is an extension you can get to make the task even easier - XDebug Helper. After it is installed, instead of typing the start message, simple click on the icon at the bottom of your browser window and your debug session will start.
The only issue I've had with this setup is that when XDebug is running and a page first loads, Apache will throw an exception. Windows (I'm running XP Service Pack 2) will ask if I want to submit an error report to Microsoft. On checking what is in the error report I see that the problem is with XDebug. I tell it not to send an error report, then get on with my work. Everything seems to work fine. I can debug PHP rather than wondering where my code is wrong. Productivity is up. My understanding improves. I can work purposefully rather than randomly.
Life is good.