The Overview
So after being told by my peers that print_r() was adequate, I embarked on a quest to get true PHP debugging working in a reputable IDE. Recently I’ve heard nothing but good things about IntelliJ IDEA 10, so I decided that would be my test subject. Don’t let the “J” fool you, while Java is its focus, this bad boy handles an array of languages and markups, including ActionScript, PHP, Groovy, Scala, Python/Jython, Ruby/JRuby, Clojure, Javascript, and HTML.
If you are a serious PHP developer and have not given debugging a fair chance, or a chance at all, you are killing yourself. So let’s do this thing from the ground up. WAMP, XDebug, IntelliJ, and magic. Follow these steps (for Windows) to reach my recent state of elightenment and PHP debugging bliss.
By the way, I almost gave up on IntelliJ entirely due to problems with debugging until Alex Petrescu () talked me down from the edge. I’m very grateful as IntelliJ is proving to be an excellent IDE.
Step By Step
- Download and install WAMP (I installed to C:\wamp)
- After installing WAMP, create a file named C:\wamp\www\info.php that contains the following code:
phpinfo(); ?>
- Open the http://localhost/info.php in a browser to get your PHP configuration.
- Now view the source of the page in your browser. Copy the entire contents of the source to your clipboard.
- Goto the XDebug Tailored Installation Instructions and paste your PHP configuration source into the text box then click “analyse my phpinfo() output”.
- Follow the resulting instructions for adding XDebug to your PHP installation. Once you make those changes, make sure the new section of your php.ini looks like this (as per a C:\wamp installation):
[XDebug] zend_extension="
" xdebug.remote_enable=true xdebug.remote_port=9000 xdebug.profiler_enable=1 xdebug.profiler_output_dir="c:\wamp\logs" - Download and install IntelliJ IDEA 10 Ultimate Edition (trial). It may not be necessary, but I included all the available extensions.
- Setup a basic PHP project in IntelliJ IDEA 10
- Goto File -> New Project…
- Select Create project from scratch and click Next
- Enter a name for your project and select Project Module under the Select Type heading and click Next
- Select “do not create source directory” and click Next
- Click Finish then OK when asked about starting a project without a JDK assigned.
- Create a PHP file to test debugging
- In the project panel on the left side, right click the project name and goto New -> PHP File
- Enter a name for your PHP file and click OK
- In the code panel on the right, enter the following and save the file:
$a = 0; ?>
- Click the vertical bar between the project panel and the code panel next to the following line to create a red breakpoint:
$a = 0;
- Copy the PHP file you created in step #9 to your WAMP www directory (C:\wamp\www)
- Right click on the PHP file name in the project panel, then goto Debug -> filename.php on server
- In the resulting window, click the dotted button next to the Server dropdown
- Setup your deployment settings
- Select Local from the access type dropdown
- Click the Mappings tab and under “web path on server” enter a single slash “/” to specify the WAMP root. Click OK and then OK again to finish the configuration.
- Right click on your PHP file in the project panel again and goto “Run on server” or “Debug on server“. I’ve had inconsistent behavior using it, so try both if one doesn’t work.
- If all went according to plan, a browser should open and execution should stop at your breakpoint. If you look at your debug panel on the bottom on IntelliJ you should now see your PHP globals laid out. Congrats, you made it!
The Summary
I know, its not exactly a walk in the park, but its well worth the work to not have to pollute your code with copious print_r() calls. You can now have full access over your PHP as you would with any other enterprise level language and debugger. I didn’t go into any specific detail here, but I plan in the future to discuss all the ins and outs of PHP debugging in IntelliJ IDEA 10. Its looks to be a great IDE thus far.
If you have any questions, which is likely since I ran into some inconsistent behavior, feel free to comment and I’ll try to answer back as quickly as possible. Hopefully you’ll never even need to ask.
Happy PHP debugging!