Saturday, 19 October 2013

Debugging PHP for FREE with Netbeans/XDebug/PHP

For a long time I've been looking to replace Dreamweaver as my main development tool, recently I've been trying various different programs such as PHPStorm, Sumlime Text. One feature I really wanted was the ability to step through and properly debug PHP code. Netbeans is a free IDE which supports this feature.

I've spent quite a lot of today getting XDebug to work with Netbeans on Windows 8.1, most of the help I found online was incomplete so I've decided to compile it here into a guide that works (at least on my workstation!).



  1. Download and install the Java runtime (I used version 7 update 45).
    http://www.java.com/getjava
  2. Download and Install Netbeans (I used version 7.4, HTML5 & PHP option).
    https://netbeans.org/
  3. Download PHP 5.4 zip file and extract to "d:\php" (I used "VC9 x86 Non Thread Safe"). Make sure to get a Windows Binary zip file.
    http://php.net/downloads.php
  4. Download and install "Visual C++ Redistributable for Visual Studio 2012", (get the x86 version regardless of your PHP choice)
    http://www.microsoft.com/en-us/download/details.aspx?id=30679
  5. Download the version of XDebug which corresponds to the version of PHP you downloaded, place the DLL file in your "d:\php" folder (I used "PHP 5.4 VC9 (32 bit)")
    http://xdebug.org/download.php
  6. Create a "php.ini" file in "d:\php" and populate it with the following (replace YOURDLLFILENAME with the precise filename of your XDebug DLL. Mine was php_xdebug-2.2.3-5.4-vc9-nts.dll). :
    zend_extension=YOURDLLFILENAME
    output_buffering = Off
    date.timezone = Europe/London

    [xdebug]
    xdebug.remote_enable=on
    xdebug.remote_handler=dbgp
    xdebug.remote_host=localhost
    xdebug.remote_port=9001
    xdebug.remote_autostart=1
    xdebug.idekey=netbeans-xdebug
  7. You can now test the PHP install by running the following from a command prompt (note the capital S!):
    D:\PHP\php -S localhost:80

    Assuming that works, push Ctrl+C to stop the service, then type 'exit'.
  8. That's everything installed, now on to the configuring. Open Netbeans!
  9. In Netbeans, go to Tools > Options > PHP > General, on this tab set 'PHP 5 Interpreter' to 'd:\PHP\php.exe'.
  10. In Tools > Options > PHP > Debugging. You shouldn't need to change anything here, the important thing is that 'Debugger Port' matches 'xdebug.remote_port' in your 'php.ini' and that 'Session ID' matches up to 'xdebug.idekey'.
  11. Create a new project, or edit the properties on an existing project. The important setting here is 'Run Configuration'. Make sure that 'Run As' is set to 'PHP Built In Web Server', Hostname is 'localhost' and Port is '80' (this doesn't have to be 80, but it must be different to the port used to send debug data).
  12. The project must have a file called 'index.php'. Create the file, enter some code and create a breakpoint (Ctrl + F8).
  13. Cross your fingers and start 'Debug Project' (Ctrl+F5) a browser window should pop up, and your code should stop at your specified break point. The Windows Firewall may pop up at this point, you don't need to grant any access for the local debugging to work.
This post was compiled from quite a few answers found on Stack Overflow, as well as some of my own experimentation/research.

1 comment:

  1. Great Post, Actually PHP is a beautiful source for developing a database driven web application, I love this post, thanks for spending your time for discussing about this topic.
    Regards,
    PHP Course Chennai|PHP Training

    ReplyDelete