Now if we are using Eclipse, then ‘debug’ mode in Eclipse is a very useful tool to debug the code. Putting prints in the code and tracking the print-outs on the server console is very tedious job. So here we are explaining; how we can configure Eclipse and PHP web server to enable debugging of PHP applications in Eclipse using XDebug (http://www.xdebug.org/docs/).
As PHP code runs on web server, so that means, debugging can be performed only on server. So we put the debugging information in PHP code file using eclipse in IDE, however we need to install XDebug on web server for actual debugging. XDebug will sit on the server, and will generate the required debugging information like variable dump, stack traces etc and put all of this information for public use on a specific port of the server. Now any application can access this debug information by sending request to server on this port.
Eclipse works on same concept for showing debugging information for PHP application. It connects to the web server on given port, and fetches the debug information to show in a user friendly way with its UI. So to configure the debug mode in Eclipse, we need to configure XDebug on web server and also need to configure the eclipse for XDebug properties. Configuration steps are as following:
Configure XDebug on server (for Window):
- Install web server (or XAMPP) on our system.
- Ensure that php>ext directory has the xdebug library. Please ensure the latest and compatible version for your current PHP installation and the OS.
- You can download the right version of XDebug from http://www.xdebug.org/download.php
- Check which php.ini (path) is in use by server by print phpinfo()
- Open php.ini and make the following changes
- Find ‘implicit_flush’ and set it to ‘on’
- Find line with zend_extension attribute and uncomment it.
- Set the value of zend_extension to the current path of xdebug library file in php > ext folder.
- Attribute name was zend_extension_ts before PHP 5.3
- Find the section starting with [zend] and comment all the lines by putting a semi-colon ‘;’ at the start of each line.
- Find the section starting with [xdebug] and uncomment all the required attributes. Mostly required attributes are:
- Xdebug.remote_enabled = true
- Xdebug.remote_host = localhost
- Xdebug.remote_port = 10000
- Xdebug.remote_handler = dbgp
- Xdebug.profiler_enabled = 1
- Xdebug.profiler_output_dir = ”C:\xampp\temp”
- Please ensure to set above properties as per your configuration, like remote host can be other than ‘localhost’.
- Now XDebug is configured on the server.
- Restart the web server.
- Print PHPInfo on browser, it can be done from control panel if you are using xampp.
- You will find XDebug section in PHPInfo page which will ensure that XDebug is configured on server now.
- Common problems
- Incompatible version of XDebug library
- Wrong php.ini
Configure XDebug in Eclipse for enabling the Eclipse Debug Mode:
- Install Eclipse (PDT) on your system. You can download it from http://www.eclipse.org/pdt/downloads/
- Create workspace on your Eclipse.
- The easy way to have PHP files run directly from your Eclipse is to create the workspace in ‘htdocs’ folder on server and then create and save the project contents in this workspace. This will automatically deploy the any new developed code to web server.
- Alternatively you can keep your source code in some other directly, and can use ant like script to deploy the final code to web server.
- Open Preference dialog from Window > Preferences
- Go to PHP > Debug node
- Set PHP debugger to ‘Xdebug’
- Ensure that ‘Break at first line’ checkbox should be checked.
- Go to PHP > Debug > Installed Debuggers
- Set the port of XDebug to ‘10000’
- Ensure that port for Zend should be other than ‘10000’, like it can be ‘10001’
- Go to PHP > Debug > Workbench Options
- Set ‘allow multiple debug sessions’ to ‘Never’
- Set ‘Switch back to PHP Perspective’ to ‘Always’
- Go to PHP > PHP Servers
- Ensure that your web server localhost address is set as Default PHP Web Server, and your workspace path should be appended to the url like http://localhost:8080/workspace
- Now create any new project. By default, it will be created in default workspace i.e. in web server’s htdocs folder and hence automatically will be deployed to server
- Create a new Test.php file
- Put some code to the file
- Put a breakpoint at first line
- Right click on the file and select to ‘Debug as > PHP Web Page’
- Eclipse should open the default system browser for the page, and the breakpoint should be triggered on first line of code.
- Now you can debug the application using various instructions of eclipse debug mode, like step-in or step-over etc.
- Common Problems
- XDebug option is not set in preferences
- Port is different than what you have set in php.ini
This is all to configure the PHP web application in Eclipse for debugging mode. Happy Debugging!!
Good day! Do you use Twitter? I'd like to follow you if
ReplyDeletethat would be ok. I'm undoubtedly enjoying your blog and look forward to new updates.