What is the best way to debug PHP code? I am using Dreamweaver for PHP development so is there some tool specific to Dreamweaver? If you have used Netbeans to debug JSP or Java code, you would know what I'm looking for.
Honestly, I find printing data into the output to be the best way to debug PHP :(
gogo printf debugging.
Absolutely not - printing is not debugging, it's poking around.
I personally use PHPEd and I've tried pretty much every PHP IDE out there. PHPEd came out on top.
PHPEd's debugger contains an amazing visual debugger that supports
- with breakpoints,
- local and global variables (including array, object handling, etc),
- watch,
- call stack,
- in-place PHP shell, which you can use to modify data as you debug along.
Another alternative is XDebug which is free and OSS. In fact, PHPEd's debugger may even be based on it, though I'm not sure.
I just stopped using Zend Development Environment (ZDE) and Zend Platform which were costly ($399 or so), but they just dropped FreeBSD 7.x support, so I dropped them. But they were very good tools and easy to use. (Although the Eclipse version of ZDE needed some work when I used it 4 months ago.)
I just switched to NetBeans and XDebug, both are free, and are on-par with ZDE and ZP:
http://www.netbeans.org/downloads/index.html
And as a final note: Degugging is not as much fun as Debugging.
Aptana Studio's PHP module is another option that includes integrated debugging. They have a free version and even their pro version is reasonably priced.
The best way to debug PHP would be to use a debugger, as others have suggested.
Personally, I use the eclipse PDT, it supports both XDebug and Zend Debugger. Zend Debugger is actually free (unlike the framework) but it's usually not as advertised. Personally I find XDebug being the better choice because it also supports profiling and code coverage. Both SimpleTest and PHPUnit require XDebug to produce code coverage reports.
Also, I would say that using printf()
to do debug is still a valid way, just not as elegant as using a debugger. But for really simple problems, we use simple tools. The better practice would be to encapsulate the debug info stuffs into a separate function so you would say debug("I am at point A")
instead of printf("I am a point A")
and later be able to disable all debug stuffs by changing debug()
You can also look at FirePHP, with which you can print debug information to the Firebug console instead of "polluting" the real output.
I've used eclipse, komodo, zend (although a few versions back), visual studio, and text editors with print/die.
I think Visual Studio with the VS.php plugin is the best debugging environment for PHP. It comes with it's own web server and there is just about zero configuration/hassle. You create your project, set a breakpoint and run the app. Intellisense works great and object/variable viewing is easy when stepping through code.
Importantly, when you remove your breakpoint, you can code/view results in the browser just as if you were using a text editor. I've found in other IDE's that it was work to manage a debugging session. With Visual Studio it's very intuitive and you just focus on developing your app.
It works with Visual Studio express, which is free, but the plugin costs about $100 US. Well worth it.
Just want to point out: all the above IDE's have Mac versions expect for VS (Visual Studio) and PHPEd. Most of them are Eclipse based IDE's.
Check out FirePHP, from the makers of Firebug. They're both handy tools.
An other php debugger is VS.PHP, really nice if you come from .Net environment. http://www.jcxsoftware.com/vs.php