In b4, “Did I just take a time machine to 2005”? PHP is still a viable language. Maybe you hate PHP and I’m fine with that but there may come a day when that Magento site you built 8 years ago goes down or the WordPress blog you set for your mom (#GreatestChildEver) needs a little bit of lovin’.
Now, PHP may be dead to you but that doesn’t mean you allowed to debug code like George Dubya was still in office. This means you var_dump($awesomeArray);exit; And you: echo ‘<pre>’; print_r($whatDoesTheFoxSay); echo ‘</pre>’; die;
Sure they still may have their uses and I’m not here to judge but there are better ways. We have the technology. I’m sure at some point you’ve used one of JetBrains’ IDEs. If not you should definitely check them out. They have an IDE specific to almost major programming language but if you found this blog via a Google Search then you already know that and are wondering why I’m still talking so let’s get started.
Mavericks, PHP5.4, and XDebug…Oh my!
First thing you may be saying is, “Why don’t you use that ZendDebugger homie?”. Zend and I have twitter beef from 2011 that we just can’t seem to squash. Just kidding…Zend Debugger’s so extension doesn’t support PHP 5.4 natively yet so we’re going to use Xdebug instead.
First step is to install Xdebug. It’s simple if you have PECL installed.
sudo pecl install xdebug
Bear in mind that pecl may need to be installed from the XCode Command Line tools prior to be able to run pecl.
Set up php.ini
You’ll need to add some entries to your php.ini file. Take note of the idekey entry. You can set it to whatever you want but we’ll need it later. If you installed the xdebug extension the way above then your path will probably match mine. If not, you’ll need to add the full path to your xdebug extension. The default listening port is 9000 feel free to change that as well.
[XDebug] zend_extension=/usr/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so xdebug.idekey=phpstorm xdebug.remote_enable=1 xdebug.remote_port=9000 xdebug.profiler_enable=1 xdebug.profiler_output_dir=/tmp
Once that’s been added go ahead and restart apache. sudo apachectl restart
Set up PHPStorm 7.0
The first thing that we’re going to do is click on the phone icon in the top toolbar. It should flip to green radio waves like this:
Ok now that we’re listening we need to tell the browser that that we’re going to sending info to the debugger. Jetbrains actually has some super handy bookmarklets to make this pretty painless. You can find them here.
Again, note the IDE key text input and make sure it matches up with your php.ini value.
- Double check your Listen for PHP Debug Connections is on in PHP Storm.
- Add a breakpoint in your code.
- Navigate to the page you want to debug and click the bookmarklet for “Start debugger” and refresh the page.
If all goes well you should hit your breakpoint and be able to hover over any variable with the scope of the function and see its value.
You’ll also have access to the variables, step-in, step-over, and other debugger functions in the Debug window.
And there you have it
As always, if you have any issues reach out in the comments or throw me a link to your Stack Overflow question. Happy hacking!