{"id":661,"date":"2009-01-26T22:00:11","date_gmt":"2009-01-27T03:00:11","guid":{"rendered":"http:\/\/unitstep.net\/?p=661"},"modified":"2009-01-26T22:00:11","modified_gmt":"2009-01-27T03:00:11","slug":"getting-xdebug-to-work-with-apachexampp-to-debug-php","status":"publish","type":"post","link":"https:\/\/unitstep.net\/blog\/2009\/01\/26\/getting-xdebug-to-work-with-apachexampp-to-debug-php\/","title":{"rendered":"Getting Xdebug to work with Apache\/XAMPP to debug PHP"},"content":{"rendered":"
I’ve written about Eclipse<\/a> and how useful it can be<\/a>, with its extensible plugin-based system. It’s so useful that I use it everyday for almost any language – Java, PHP<\/acronym>, JavaScript to name a few. It’s even great for things like CSS<\/acronym> and XHTML<\/acronym><\/a>. <\/p>\n PHP<\/acronym> is currently my favourite “hobby” language and has been for some time. While I like PHP<\/acronym>, one of the things that hasn’t been straightforward with it is setting up a proper debug session, where you can step through code. This contrasts heavily with a language like Java, which has always had strong developer tools. This has resulted in a mass of third-party tools aimed at facilitating PHP<\/acronym> debugging. A while ago, a reader emailed me asking about this very topic, so I decided to put together how-to detailing my experience with the topic and how I went about learning it.<\/p>\n <\/p>\n The debugger I’ll be using will be Xdebug<\/a>. Because PHP<\/acronym> provide no built-in debugging tools, there are many third-party options for debugging. (See the “Debugging Tools” section of this article<\/a> for more) However, Xdebug seems to be one of the more popular ones, and Eclipse PDT<\/a> already has support for it.<\/p>\n This guide also assumes use of XAMPP<\/a>, the great all-in-one solution for quickly setting up a web development environment and to get your code running on the server. XAMPP is great for hitting the ground running, though you’ll probably not want to use it in a production environment – though you likely won’t be debugging there either. Nevertheless, the instructions provided here should work even if you’ve setup Apache and PHP<\/acronym> separately on your own.<\/p>\n The first thing you’ll want to do is head over the Xdebug<\/a> page and download the appropriate Zend extension of Xdebug corresponding to the version of PHP<\/acronym> you’re running. Save the file into your PHP<\/acronym> extension path\/folder. Now you’ll have to edit your You’ll also want to have downloaded Eclipse PDT<\/a> have that installed as your IDE, if you haven’t already done so. Zend Studio for Eclipse<\/a> also works, since it’s based on Eclipse PDT, and offers quite a few more features, out of the box.<\/p>\n You should have already saved the Xdebug extension DLL file to your PHP<\/acronym> extension folder. Record down the full path of it. Now, open up your The Then, you should disable the Xdebug entry in the list of dynamic extensions<\/strong>. This is confusing, but since we are already setting up Xdebug as a Zend extension, we don’t need another entry. Disable the Xdebug dynamic extension by ensuring the following line is commented out, like below:<\/p>\n There is one last very important step you need to do, particularly if you are running XAMPP. Current versions of Xdebug are incompatible with the Zend optimizer that is enabled by default in XAMPP, so you must disable that if you want Xdebug to work<\/strong>. If you don’t, you’ll notice that Apache will crash every time you try to load it with Xdebug enabled. To disable the Zend optimizer, find the You should be able to start Apache now without troubles.<\/p>\n The next part will be configuring Eclipse as a debugging client. Since the code will be executing on the web server (Apache), you’ll need Eclipse to “hook in” using the Xdebug protocol. Thankfully, configuring Eclipse is fairly straightforward.<\/p>\n Open up Eclipse’s preferences and go to PHP -> Debug<\/strong>, and ensure that XDebug is selected as the PHP<\/acronym> debugger. This sets the default for debugging sessions and lessens the configuration required for each debug session. You can also make sure that the default web server is Note that if you are running Zend Studio<\/strong>, you’ll need to follow the steps in this article to enable Xdebug support<\/a>. It seems that some versions of Zend Studio by default disabled support for the Xdebug plugin in lieu of their own Zend Debugger. <\/p>\n \n Now you can select a file from a project you’d like to debug. In my case, I’ve selected \n Double click the the “PHP<\/acronym> Web Page” entry on the left side bar to create a new debug profile. Here, I’ve named it “CHAP-PHP”. You should see a dialog like the one above. Make sure the “Server Debugger” is again set to Xdebug and that the PHP<\/acronym> Server is set to the localhost configuration you set up previously. <\/p>\n Then you have to select the file you want to debug. Click on “Browse”, and you’re confusingly taken to another view of your Eclipse projects; simply select the same file as before – you have to select a specific file, and not just a project or folder.<\/p>\n After that, you’ll need to adjust the URL<\/acronym> mapping. You’ll probably need to uncheck “Auto Generate”, and then enter the URL<\/acronym> that corresponds to the PHP<\/acronym> file you’re debugging<\/strong>. Here, I’ve manually entered If you want the debugger to stop right at the first line to allow you to immediately begin stepping through code, check “Break at First Line”. (It may be checked by default) Otherwise, uncheck it if you only want the debugger to stop at the breakpoints you’ve specified in Eclipse, which is the normal behaviour most developers will expect. <\/p>\n You should now be able to click “Debug”, and a debug session will launch, opening up the URL<\/acronym> you’ve specified and allowing you to step through code. If you don’t like Eclipse using its own internal web browser (which appears just be a front for IE), you can configure which web browser you’d like it to launch URLs with by opening up Preferences and then going to General -> Web Browser<\/strong> and changing the setting to use an external web browser of your choice. Personally, Firefox is my preference.<\/p>\n You can now get acquainted with stepping through code, which in my opinion, is one of the best ways to learn! When you launch a debug session, Eclipse should prompt you to switch to a new “perspective”, which is just a different layout of Eclipse’s internal windows that many believe better suit debugging through code. <\/p>\n \n You’re provided with an informative view of the script your currently debugging, along with the highlighted line that execution has paused on. You can set debug breakpoints by double-clicking in the left margin of your source code view window; debug breakpoints show up as blue circles here. The buttons at the top (green “Play”, red “Stop” and others) provide control over execution of the code, allowing you to step through code line-by-line, step into functions\/methods and return from them. I encourage you to experiment with all of the controls and get acquainted with the keyboard shortcuts.<\/p>\n Another panel also shows all the current variables available to the script as well as their values. This is useful since you now do not need to When you’re done, you can just click the red “stop” button to disconnect from the server and end the debug session. If you’ve completely stepped through a script, you will not be automatically disconnected from the server. Instead, the debug client in PHP<\/acronym> will patiently wait to debug the script again the next time it is executed. This is useful to know, since you can just go back to the URL<\/acronym> in your web browser and reload the page to trigger the debug session to resume again. <\/p>\n I hope you found this useful, as when I was starting out trying to get a PHP<\/acronym> debug session to work, it was somewhat frustrating. As always, I welcome your comments, suggestions and questions below via the comments form!<\/p>\n I’ve written about Eclipse and how useful it can be, with its extensible plugin-based system. It’s so useful that I use it everyday for almost any language – Java, PHP, JavaScript to name a few. It’s even great for things like CSS and XHTML. PHP is currently my favourite “hobby” language and has been for […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[318,208,200,64,6,137,277],"tags":[],"_links":{"self":[{"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/posts\/661"}],"collection":[{"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/comments?post=661"}],"version-history":[{"count":31,"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/posts\/661\/revisions"}],"predecessor-version":[{"id":701,"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/posts\/661\/revisions\/701"}],"wp:attachment":[{"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/media?parent=661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/categories?post=661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/tags?post=661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}<\/a><\/p>\n
Xdebug for PHP<\/acronym> and XAMPP<\/h2>\n
Getting started<\/h2>\n
php.ini<\/code> file to begin using the plugin. The plugin basically exposes or provides an interface for the client debugger (running in Eclipse or your IDE) to attach to the server and debug\/trace through the code that’s running on it. If you’re from the Java world, you’ll know this as “remote debugging”, which is provided by most J2EE application servers.<\/p>\n
Setting up Xdebug<\/h2>\n
php.ini<\/code> file and go down to the
[XDebug]<\/code> section, or create it if it’s not there. Uncomment or add the following lines:<\/p>\n
;; Only Zend OR (!) XDebug\r\nzend_extension_ts=\"D:\\XAMPP\\php\\ext\\php_xdebug.dll\"\r\nxdebug.remote_enable=On\r\nxdebug.remote_host=\"localhost\"\r\nxdebug.remote_port=9000\r\nxdebug.remote_handler=dbgp<\/code><\/pre>\n
zend_extension_ts<\/code> should point to location of your Xdebug extension DLL that you downloaded earlier; modify as appropriate.<\/p>\n
;extension=php_xdebug.dll<\/code><\/pre>\n
[Zend]<\/code> section in
php.ini<\/code> and comment out all of the entries under it, like so: (This is an example, there may be more to comment out)<\/p>\n
[Zend]\r\n;zend_extension_ts = \"D:\\XAMPP\\php\\zendOptimizer\\lib\\ZendExtensionManager.dll\"\r\n;zend_extension_manager.optimizer_ts = \"D:\\XAMPP\\php\\zendOptimizer\\lib\\Optimizer\"\r\n;zend_optimizer.enable_loader = 0\r\n;zend_optimizer.optimization_level=15\r\n;zend_optimizer.license_path =<\/code><\/pre>\n
Configuring Eclipse for PHP<\/acronym> debugging<\/h2>\n
localhost<\/code> if that’s the case, which it’ll likely be for a lot of people doing development.<\/p>\n
<\/a>\n<\/p>\n
src\/demo\/index.php<\/code> from my Challenge-Response PHP<\/acronym> Login System<\/a> project. Open the file, and then go to the Run Menu<\/strong> and select Debug Configurations…<\/strong> or Open Debug Dialog<\/strong>.<\/p>\n
<\/a>\n<\/p>\n
\/projects\/CHAP\/trunk\/src\/demo\/<\/code> as the URL<\/acronym> fragment that triggers execution of the script.<\/p>\n
Start your debugging engines!<\/h2>\n
<\/a>\n<\/p>\n
echo<\/code> anything to output or change any of the code to see values. <\/p>\n
Conclusion<\/h2>\n
References<\/h3>\n
\n