views:

2016

answers:

9
+7  Q: 

Debugging PHP code

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.

A: 

Honestly, I find printing data into the output to be the best way to debug PHP :(

gogo printf debugging.

Paul Tarjan
Why downvote this? It is a valid way to find bugs. Until the Original Questioner add "I don't want to print to the screen" to the question, this is a valid form of debugging.
jmucchiello
Printing to the screen -certainly on the development server/environment- is a superb way of seeing what's happening throughout the script; my own use is <?php echo "$variable (" . gettype($variable) . ")<br />\n"; ?> but maybe I'm crazy =)
David Thomas
While printing out data works, it's not the most efficient way, I'm down voting this because, despite doing it myself at times, it's not a great answer to the problem.
TravisO
After spending years doing printfs to debug, once I had a "true" debugger, it's like crawling your entire life then learning to walk. So, I agree with TravisO that printfs are not a great debugging technique. Plus, more than once I checked in code with printfs in them accidentally (in edge cases, etc.).
razzed
+5  A: 

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.

Artem Russakovskii
I've been programming PHP for a long time and printing at interesting points solves 99.9% of my bugs. I'm not sure if that makes me a good or a bad programmer...
Paolo Bergantino
Debugging is a lot more elegant and practical than printing - not only you can see all the variables, no matter if you remembered to print them or not, you can manipulate them on the fly. Until you try it, you don't really realize how much power PHP debugging offers, I highly recommend you give it a go first.
Artem Russakovskii
+1 for PhpED...
Dana Holt
Agree with Artem Russakovskii 100%. Used to use a text editor for PHP, and it took me a while to get to get used to PHPEd, but once I did I could never go back. The debugging is a lot faster than having to type echos and print_rs all over the place. Just be sure you have the debugger set not to stop on the first line of the file on every run, or it will drive you crazy. Turn that off, and it will only stop if you add a breakpoint to the code you are working on.
Eli
Also, the profiler that comes with it is pretty nice.
Eli
If only I could figure out how to make it not explode when debugging AJAX calls that run while the original page is still being debugged...
Artem Russakovskii
I prefer phpDesigner, everytime I hear some say they've "used every IDE out there" more often than not they never tried phpDesigner, and more than a few times people that I recommended it to, loved it. It has built in support for Xdebug and it's very cheap, like ~$55usd.
TravisO
I tried phpDesigner and didn't like it one bit - the interface is so much more awkward than PHPEd.
Artem Russakovskii
+3  A: 

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

http://www.xdebug.org/

And as a final note: Degugging is not as much fun as Debugging.

razzed
+1 for NetBeans - their PHP and PHP debugging support is definitely on par with the second best PHP IDE, Zend Studio. What's the best PHP IDE? NetBeans, obviously. =)
Jani Hartikainen
A: 

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.

http://www.aptana.com/php

borodimer
+3  A: 

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.

kizzx2
A: 

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.

Steve
A: 

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.

Stephen Cox
A: 

Check out FirePHP, from the makers of Firebug. They're both handy tools.

mlambie
A: 

An other php debugger is VS.PHP, really nice if you come from .Net environment. http://www.jcxsoftware.com/vs.php

cristi