If you write PHP on a Windows computer then setting up a reliable method for debugging your scripts will at some point be something you will want if not need to do for when simply writing out variables to the page will not do.
- Windows PC
- WAMP Server (Windows, Apache, MySQL, PHP)
- Eclipse IDE
- PHP Debugger
Before we begin I suggest you make a new folder on your C drive called PHPDebugging to store all the files you need to download for this tutorial, also make sure your browser asks you where to save the files you download (tools >> options >> etc) as this will make the whole process allot easier.
First make sure that you have WAMP installed, XAMP may also work in the same way but I have used WAMP so my instructions are based on this.
Next go to http://www.eclipse.org/pdt/downloads/ and download the "Eclipse PDT 2.2.0 All In Ones / Eclipse PHP Package" - "All-In-One Windows 32-bit" file.
Unpack the Eclipse file and open the eclipse.exe file. You will need the Java Run Time environment installed to be able to open eclipse so if you don't have that then download it direct from Sun (http://www.oracle.com/technetwork/java/javase/downloads/index.html).
The first time you open Eclipse there is a splash screen and you will need to open the firewall for it as well. On the splash screen go to "Open Workbench".
The default PHP Development Tools (PDT) are not part of the default Eclipse package so click on the Help menu and select "Check for updates" and install "Eclipse IDE for PHP Developers" and restart Eclipse when prompted.
The next stage is to configure your WAMP installation to use Xdebug. Go to http://xdebug.org/find-binary.php and paste the output of phpinfo() in to the form, this will tell you exactly which Xdebug file you need to download if you do not already have it installed. If you don't have it already then follow the instructions on that page after submitting your system info.
If you do not have a file set up to output the contents of phpinfo() then just create a new file with notepad and copy the following code in to it and save it here "C:\wamp\www" and call it phpinfo.php
To access this file via the Apache server (ie to see the output) first make sure that your WAMP Apache server is online, Go to the WAMP tray icon and click on "Put Online". Next go to http://localhost/phpinfo.php in your browser. This file can also serve as the file to test the debugger later on.
Having pasted the output from your phpinfo file in to the Xdebug form, one thing to note is that the instructions I received whilst doing this were not enough to get Xdebug working completely. Stage 3 of the instructions should tell you to open your php.ini file and add one line. I recommend that you add the following. Remember to replace the line with the path to the .dll file with the line from the Xdebug instructions you received following the previous step.
- ;; Only Zend OR (!) XDebug
- ; zend_extension_ts="C:xamppphpextphp_xdebug.dll"
- zend_extension_ts = c:\wamp\bin\php\phpXXX\ext\php_xdebug-XXX.dll
- xdebug.remote_host=127.0.0.1 ; if debugging on remote server,
- ; put client IP here
Now we are ready to see the debugger in action. Restart your WAMP server and Eclipse and reopen Eclipse. You will need to have a PHP project set up so do this if you do not already have one set up
- Go to File >> New >> PHP Project
- Give your project a name
- Select the option to create project from existing source
- Browse to the root of the site you want to manage
- Click OK
Now you have a project set up you need to set up some of the preferences in Eclipse so go to Window >> Preferences, Expand the PHP menu item and go to the PHP Executables option and point Eclipse to your php.exe file located in the "WAMP/bin/php/version/" folder. now select the Debug option and Select Xdebug as your debugger and give your server a name.
Now you need a debugging profile so expand the debugging menu (it has a picture of a bug next to it, top left) and go to the Debug Configurations option. Select the PHP Web Page option set up a new configuration with the correct debugger, server and file information. If you select the Break at first line option then the debugger should fire up on the first line of PHP so you will be able to see if it is working without setting any breakpoints.
These settings don't get automatically generated correctly. If you created the phpinfo.php file and saved it in the www folder of your WAMP installation for example, created a project called "test" and then used this as the file to debug then you will need to un-check the URL Auto Generate option in the configurations panel and remove "test" the URL as this will not be the URL of the page you are trying to debug (edit it to "/phpinfo.php").
Click Apply and then click Debug. You should now be in debug mode effectively debugging your 1 line script. You can also open the debug menu again and click on the profile you just created that should open that file or page in the debugger and you can step through each line of code.
This is by no means a tutorial about how to write PHP and I assume that if you are reading this then you have at least a little coding experience. The main thing I hope you can gather from this is the basic principles of how to set up PHP debugging on a windows machine.
Eclipse it's self is a very powerful IDE and as well as enabling debugging it is definitely a good tool to use whilst writing PHP as you not only have the Debugger built in but also a whole host of other useful features that other programs such as notepad++ or Dreamweaver do not have.
Just a quick update with my most up to date xDebug settings from php.ini.
I had noticed that my WAMP\tmp folder contained 220GB of log files so have disabled the logging and updated to WAMP 2.1 with PHP 5.3.4
- ; XDEBUG EXTENSION
- zend_extension = "c:/wamp/bin/php/php5.3.4/zend_ext/php_xdebug-2.1.0-5.3-vc9-x86_64.dll"
- xdebug.remote_enable = On
- xdebug.profiler_enable = Off
- xdebug.profiler_enable_trigger = Off
- ; xdebug.profiler_output_name = cachegrind.out.%t.%p
- ; xdebug.profiler_output_dir = "c:/wamp/tmp"