Debugging is an invaluable part of software development. I find it very useful in a variety of situations, for instance when I want to understand how a routine works or I need to get rid of a bug that is not exactly easy to fix just by reading the code.
There are several ways to perform debugging in PHP:
pecl install xdebug
If the above does not work, check the Xdebug installation instructions at http://xdebug.org/docs/install .
Once the xdebug extension was installed, you will have to add the extension to php.ini. Add the following lines to php.ini:
[xdebug]
zend_extension=/path/to/xdebug.so(.dll)
There are several ways to perform debugging in PHP:
- The most straightforward technique is to use print_r() and var_dump(). This will alter the output, it's quick but very dirty. If you're using this there's nothing to be ashemed of, everyone is doing it.
- Logging into files/database tables at specific points in the code. This is cleaner than the previous method, but it requires additional effort and usually polutes the code with logging routines. Also this is not exactly debugging, it's logging and analisyng the logs.
- Using proper debugging tools like Xdebug or the Zend Debugger, integrated into your PHP IDE. This is the clean way to do it, it provides a much better insight into the source code, as you can run it interactively, step by step.
pecl install xdebug
If the above does not work, check the Xdebug installation instructions at http://xdebug.org/docs/install .
Once the xdebug extension was installed, you will have to add the extension to php.ini. Add the following lines to php.ini:
[xdebug]
zend_extension=/path/to/xdebug.so(.dll)
xdebug.remote_enable=On
xdebug.remote_handler="dbgp"
xdebug.remote_mode="req"
xdebug.remote_port=9000
xdebug.remote_host="YOUR.IP.GOES.HERE"
xdebug.remote_log=/path/to/xdebug_remote_log
xdebug.remote_handler="dbgp"
xdebug.remote_mode="req"
xdebug.remote_port=9000
xdebug.remote_host="YOUR.IP.GOES.HERE"
xdebug.remote_log=/path/to/xdebug_remote_log
Update:
On Windows+PHP 5.2.14 I had to replace zend_extension with zend_extension_ts:
zend_extension_ts="c:\\php\\ext\\php_xdebug-2.1.0-5.2-vc6.dll"
Be extra careful with xdebug.remote_host, this is the host where you develop and run your Eclipse, and PHP will try and connect to Eclipse when debugging is enabled. Also make sure that the zend_extension part was not added automatically by the installation, if it was don't add it again.
If there is any mention of the Zend debugger in you php.ini file, you will have to comment that. Restart Apache or whatever web server you're using and make sure the Xdebug installation was correct by running a simple PHP script that contains phpinfo() and searching for "xdebug".
Now the tricky part, Eclipse has to be configured to accept debugging sessions from XDebug. Follow the steps below:
- Open your project in Eclipse PDT
- In the main menu select Project->Properties
- On the left side of the window select "PHP Debug" and then click on "Configure Workspace Settings"
- On the "PHP Debugger" dropdown select Xdebug and click "Apply"
- Click "Configure" to the right of Xdebug in the same window.
- Select Xdebug and click "Configure".
- On the "Accept remote session(JIT)" select "any" and click "OK". This is extremely important and this is where most people get stuck.
That's it, Eclipse is now configured, now all we need is to be able to be in control of our debugging sessions. For this we will need to install a Firefox extension called "easy Xdebug"(yes Firefox, you're not developing PHP in IE are you?).
The extension can be installed from https://addons.mozilla.org/en-US/firefox/addon/58688/ . If the link does not work just google "firefox xdebug". Install the extension and restart Firefox. After that you will notice a little green bug on the bottom-right of Firefox and if you hover it it says "Start xdebug session".
As a side note, you might have used Zend IDEs where the debug process starts from the IDE. In Eclipse PDT the process is reversed: you start from the page that you want to debug and PHP will connect to Eclipse in order to establish a debug session. That is why we have installed the firefox extension, because the debug starts from the browser.
Now open the page that you want to debug, on the server where you have just configured PHP with the XDebug extension of course. Click on the green bug I just mentioned to enable debugging and then reload the page. After this you will have to go to Eclipse and see that a new window has just popped up, asking you to "Select the local resource that matches the following server path". In a simple setup you will have just a single option, select the PHP file in that window and click "OK". Eclipse will ask you if you want to change to "PHP Debug perspective" and obviously you have to say "Yes". Optionally you can also check "Remember my decision". After this you should be in the debugging perspective, with Eclipse stopped on the first line of your code, meaning that you can now step through your code.
As a simple guideline you can use the following keys:
That's it, I'm sure you'll realise that you can't live without debugging once you start using it.
The extension can be installed from https://addons.mozilla.org/en-US/firefox/addon/58688/ . If the link does not work just google "firefox xdebug". Install the extension and restart Firefox. After that you will notice a little green bug on the bottom-right of Firefox and if you hover it it says "Start xdebug session".
As a side note, you might have used Zend IDEs where the debug process starts from the IDE. In Eclipse PDT the process is reversed: you start from the page that you want to debug and PHP will connect to Eclipse in order to establish a debug session. That is why we have installed the firefox extension, because the debug starts from the browser.
Now open the page that you want to debug, on the server where you have just configured PHP with the XDebug extension of course. Click on the green bug I just mentioned to enable debugging and then reload the page. After this you will have to go to Eclipse and see that a new window has just popped up, asking you to "Select the local resource that matches the following server path". In a simple setup you will have just a single option, select the PHP file in that window and click "OK". Eclipse will ask you if you want to change to "PHP Debug perspective" and obviously you have to say "Yes". Optionally you can also check "Remember my decision". After this you should be in the debugging perspective, with Eclipse stopped on the first line of your code, meaning that you can now step through your code.
As a simple guideline you can use the following keys:
- F5(Step Into) - steps into everything including function or method calls
- F6(Step Over) - walks through but does not step into function or method calls
- F8(Resume) - runs until the first breakpoint or end of the program
That's it, I'm sure you'll realise that you can't live without debugging once you start using it.
Good tutorial.
ReplyDeleteI would ask why not configure Xdebug ip proxy (if it is not localhost) and port inside Eclipse and run the site entry script with [Debug as > PHP Web Page]?
On the first run you will be able to specify script URL and Eclipse will automatically jump to PHP Debug Perspective and show the output in the embedded Firefox/Gecko pane (it will be replaced with WebKit in the future releases).
It looks safer than using JIT.
The approach presented in this post also works for non-webpage applications for instance command line applications or web services. In the case of command line application I want to issue the command on the server side and debug it from Eclipse. Also if I have a web service I want to call it from a GUI(like soapUI) and debug in Eclipse. I'll definately have another post on that.
ReplyDeleteGreat article! Really helpfull. Thanks
ReplyDeleteI have tried the steps of the tutorial but I still cannot debug PHP files.
ReplyDeleteI have installed xdebug over an old (non-functional) installation using PHP PECL. I have installed Apache on my local development computer so I set the variable xdebug.remote_host to 127.0.0.1.
When I call phpinfo() there appear two or three lines which include the text "xdebug" (these lines are somehow related to cookies). Is this an indication that the installation of xdebug was sucessful?
When I call Firefox I get the green bug in the corner. Clicking on the green bug adds a tiny red circle to the green bug.
However, when I refresh the page to be debugged I get no effect of the debugger: the page is displayed as usual. There is no effect in eclipse.
What did I do wrong? Thanks
Thanks for this great tutorial. With this I got it working :-)
ReplyDeleteI am using MAMP as a server. With the default installation zend debugger is actived. To get it working I had to comment out all related entries in php.ini. (May be this is the issue with Johann's post.
mschwemer:
ReplyDeleteIt is possible that this is the problem. I have installed Apache and PHP using xampp. The installation was rather straight forward: I just pressed several times return. Anyway it seems that at least Apache and PHP appears to work correct.
I was a bit confused that I did not find a section [Zend] in the PHP.ini file. So,I could not delete it.
So,I just added the section for xdebug in my php.ini
[XDebug]
;changes JR %%%&
zend_extension_ts="D:\xampp\php\ext\php_xdebug.dll"
xdebug.remote_enable=true
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.profiler_enable=1
xdebug.profiler_output_dir="D:\xampp\tmp"
In a tutorial at latenightpc.com I read that activating both debuggers, Zend and xdebug, should result in an error message - not just in "doing nothing". Since I did not get any error message I doubt somehow that the problem is the (possibly existing) Zend debugger entry.
Perhaps the installation is correct and I just don't know how to activate the debugger?
To get it right:
(1) I open my script in eclipse.
(2) I switch to Firefox and call my script using localhost.
(3) The script appears on the screen (without any debugger).
(4) I click the button with the green bug.
(5) the green bug gets a tiny red cicle.
(6) I reload the script clicking the refresh button in firefox. Should the script be reloaded or should the debugger block the reloading of the script (awaiting debugger commands like goto breakpoint, step into function and that like?
(7) I turn back to eclipse ... And there I should get a debugger window - or what?
Johann
it solved my problem, nice post
Deletecoool works for me
ReplyDeleteI tried to debug a soap server to call the soap client in browser. The client call the server with a XDEBUG_SESSION_START=ECLIPSE_DBGP parameter in the service URL. But it didn't work.
ReplyDeleteReading this post and setting the JIT to any, it starts to work. Thank you.
Really helpful, I got it working in no time. Thanks a lot!
ReplyDeleteI ran into this gotcha:
ReplyDeleteDebugging the page the first time creates a launch configuration. When tried to debug the first time I had Zend Debugger selected instead of XDebug. Couldn't figure out why my configuration changes weren't working - then I realized I was just rerunning the launch with the old config. Went into Debug Configurations and deleted the launches, bingo - everything worked.
So if you're trying to change the Workspace or Project debug settings, don't forget to change or remove any existing launch settings.
Hope this helps someone else.
Still to no avail..
ReplyDeleteI'm working on ZendStudio 8 (on eclipse),
+ xdebug shows up in phpinfo
+ zendstudio is listening on the port, with JIT set to any
and still there's no pop-up in zendstudio..
Nice tutorial....thanks for this post.....
ReplyDeletePlease visit this link for learn more script.
http://amitmondal.wordpress.com/
too bad there is no notepad++ on linux :( It actually has nice dbgp xdebug plugin.
ReplyDeleteI find that Komodo IDE is the best all around php IDE on Linux.(the only one that's not Java based)
Nice article , just to add I would suggest start up script to put JVM debug parameter and use a variable e.g. isDebugEnabled and also REMOTE_DEBUG_PORT in the script and export this variable when you want to remote debug your Java application. This will be very handy and will require just one time setup work.
ReplyDeleteThanks
Javin
How to setup remote debugging in Eclipse
Excellent article! Thanks!
ReplyDeleteIf you are using Ubuntu and the xdebug extension from the repositories, don't edit php.ini, instead edit /etc/php5/conf.d/xdebug.ini
The first line will already be added for you.
After searching & trying for 2 days the first time i'm able to debug really every file using eclipse/xdebug.
ReplyDeleteVery helpful article, thanks!
Would you care to contribute docs to https://github.com/derickr/xdebug.org/blob/tutorials/html/docs/tutorials/eclipse.rest perhaps?
ReplyDeletecheers,
Derick
Actually that's a good idea, I will.
ReplyDeletethank you Bogdan. This post helped me.
ReplyDeleteThank you so much. This tutorial really helped me to configure remote debugging on my system
ReplyDeleteThank you, still a useful posting. Following your instructions (especially the important and non-intuitive part about Xdebug calling back to Eclipse) was key.
ReplyDeleteHi, I've been wrestling with this for some time and have had no luck. Your tutorial got me a bit farther to the answer, but I'm stuck near the end.
ReplyDeleteI've done all of the setup, and when I refresh the .php file in firefox this should happen: "After this you will have to go to Eclipse and see that a new window has just popped up, asking you to "Select the local resource that matches the following server path".
Instead, firefox just sits and waits for a connection forever and Eclipse sits there doing nothing. How do I ensure that Eclipse and firefox (and MacGDBp) are all talking to eachother?
Very good ideas and thoughts are submitted that would be great use here.Thanks for this useful information here.
ReplyDeleteweb designing company
Really great post, Thank you for sharing This knowledge. Excellently written article, if only all bloggers offered the same level of content as you, the internet would be a much better place. Please keep it up!!!
ReplyDeleteStromectol 12mg
wow....! Very nice and useful article. Your way of explanation is beautiful. I learn a lot of things from your article. The stuff you are using that is very useful and helpful. Thanks for sharing a very informative article.
ReplyDeleteuplifting quotes
Nice and a very good opportunity posted for freshers and experienced candidates. You may have a look at the website mentioned here.
ReplyDeletebest web hosting service in india
Hi, I got a problem. I use eclipse for php and wamp. I could
ReplyDeletesee xdebug in phpinfo,and I could debug the php file as script,but when i debug the file as web page, nothing happened.So could you give me some advice?
It took me time to analyze most of the feedback, but I honestly love the content. The idea proved to be very useful to me and I am sure to most of the bloggers out there! It feels good when you can not just be told, but you will also be entertained! I’m confident you had felt joy penning this post.Buy Stromectol
ReplyDeleteVery interesting site. I simply got very little to do therefore I am just killing time surfing around the net and I feel like I've got discovered some very valuable site. Thanks a lotbuy propecia
ReplyDeleteYour article is very informative and unique. Thanks to you who has provided the intricate information in simple words. essay service uk formating topics
ReplyDelete