Debug PHP With XDebug On NetBeans

Udemy.com Home page wide-banner

NetBeans is a fantastic free IDE for not just PHP, but other languages like HTML, JavaScript, C++ and more. For PHP it comes highly prepared for being your predominant development environment. It has a whopping list of features that turbo-charge your programming like it’s auto-complete ability to name just one. A PHP development environment isn’t complete unless you have the power to debug the code you write, in this tutorial, you’ll learn how to correctly set that up.

What you’ll need for this tutorial

As a minimum, you’re going to need PHP already installed with a working web server. For this tutorial, I am working with PHP 7.0 and XAMPP. The PHP version doesn’t make a blind bit of difference though, XDebug comes in a variety of versions so there will be one available for your PHP build.

In addition, the although extremely obvious, you will need NetBeans installed too, making sure the PHP bundle is selected for the download.

Let’s Start

Step 1 Download XDebug

To make sure the correct XDebug module for your version of PHP is sitting in your modules directory, follow the instructions below. If you’ve already done this though, feel free to skip them.

If this is a brand new project, create an index.php file in the root directory of your web server, and add the following code. If not, simply add the following code to a file of your choice.

<?php 
phpinfo();

?>

Run the index.php or other self-specified file in a browser, preferably, Google Chrome. This will render the following information as shown in the image below –

phpinfo example

Once this is loaded up, follow these sub-steps.

  1. Highlight all of the information on the page and copy it.
  2. Go to the XDebug Wizard tool.
  3. Paste the information into the Text Box input on the wizard tool page.
  4. Locate and click the ‘Analyse my phpinfo() output’ button.
  5. Download the provided module under Instructions, number 1. (See image below)

xdebug wizard instructions

Follow the remaining instructions in the image above but remember, in this example, the directory path may be different to your own. Do not forget to restart the webserver after adding the module to the ext directory.

Step 2 Configuring XDebug in the PHP.ini file

After following the last section, you will of already added the path to the new XDebug module that was downloaded. Your PHP.ini should now have something like the following –

[xDebug]
zend_extension="php_xdebug-2.6.0-7.0-vc14.dll"

Some more configurations need to be set within the PHP.ini, add the following underneath the current setting.

xdebug.remote_enable=on
xdebug.idekey = netbeans-xdebug
xdebug.remote_host = "localhost"
xdebug.remote_port = 9000

Now the full XDebug settings within your PHP.ini file should look similar to the following –

[xDebug]
zend_extension="php_xdebug-2.6.0-7.0-vc14.dll"
xdebug.remote_enable=on
xdebug.idekey = netbeans-xdebug
xdebug.remote_host = "localhost"
xdebug.remote_port = 9000

Step 3 Checking configuration has been activated

Follow the next list of numbered steps to double check everything we’ve done up to now has been set and activated.

  1. Restart the web-server.
  2. With the browser, navigate to the file that has the phpinfo() function within it.
  3. Look to make sure there is a section titled xDebug.

The section titled xDebug should look similar to the following screenshot if it’s installed and configured correctly.

xdebug php settings

 

Note: If you are not seeing something similar as above, then you will need to follow steps 1 & 2 again.

Step 4 Configuring NetBeans to use the xDebug module

  1. Open NetBeans
  2. Go to the tools drop down and select options
  3. Click the PHP tab from the top navigation pane
  4. Click Debugging from the sub-navigation pane
  5. In the debugger port field enter 9000
  6. In the session ID field enter netbeans-xdebug
  7. Click Apply, then OK.

If you open the debugging pane of the PHP options window again, it should look like the following –

netbeans php debugging configuration screenshot

Step 5 Debug Some Code

Lets now test the NetBeans IDE in debug mode, follow the next steps to add a break point and execute the debugger.

  1. Open the index.php file of your project, add a new variable for example, $test = "BreakPointTest";
  2. Add a break point by either double clicking the line-number where the new variable is or, click the variable so the cursor is on it and press CTRL+F8
  3. This line will now be highlighted in red.
  4. Go to the debug drop down at the top of the IDE.
  5. Click Debug File.
  6. Minimize the browser window that just opened and go back to the code.
  7. There should be a green highlighted line at the top of the code, press F5 to continue to the break point.
  8. NetBeans will now of broke at the break point you set on the new variable.
  9. Complete!

See the image below to what you should be seeing on your own computer –

netbeans debugging example

Conclusion

With this article you should be able to get NetBeans debugging your code in no-time. Debugging with IDE’s is a vital part of development so it’s always good to have this environment ready. NetBeans is free, so why not use it?

Interesting Links

Any trouble setting it up, leave a message in the comments and I will get back to you promptly.

Debug PHP With XDebug On NetBeans
Posted in PHP

You May Also Like

Leave a Reply

Your email address will not be published.