Creating and Debugging PHP Project in Netbeans Using XDebug

The Installation and configuration  of XAMPP and XDebug was explained in previous openpeta post .In this article ,  Step by step procedures are given below about how to develop and debug PHP application using netbeans and xdebug.

Step 1: start your XAMPP server

# sudo su

# /opt/lampp/lampp start

Step 2: Create one directory under htdocs folder and make it writable

# mkdir /opt/lampp/htdocs/debug

# chmod 777 /opt/lampp/htdocs/debug/

Step 3: Start your Netbeans IDE and Create New PHP Project

This is just a sample program for PHP debugging, But in your real time project xdebug would be more useful and reduce lot time instead of finding error and correct manually.

Project Name : Debug [ It can be any name]

Project Path: /opt/lampp/htdocs/debug

url: http://localhost/debug

Step 4: Once you created a new PHP project, Now its time for creatign a PHP program, Right click on the project folder and select PHP file option.

File Name : Factorial.php

Contents:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
/*
 * Author : Anthoniraj Amalan
 * Date   : 20 Sep, 2010
 */
function fact($n)
{
    if($n == 1 || $n == 0)
        return 0;
    else
       return $n * fact($n - 1);
}
echo "The factorial Value of 10 is ". fact(10);
?>

Step 5: Open Your Web Browser and type the url http://localhost/debug/Factorial.php. You will get the output as 0. [ In this program,  intentionally i have given return 0 for debugging purpose]

Step 6: Debugging is the technique for finding the error in your program. You need some break points to start debugging   process in your program. In Netbeans simple click the line number where you want to start the debug in your PHP program.

Step 7: Select “Debug-> debug File” Option from netbeans main menu, Now IDE will automatically start your web browser and will wait for your command. There are three main options in Debugging

  1. Step Over [F8]
  2. Step Into [F7]
  3. Step Out [Ctrl+F7]

Step Over option will simply move your interpreter into next line of your program whereas step into will get into the particular line or function so you can easily find the error in your function. Step out is just opposite of step into option.

Step 8: Debugging Variables

In factorial Program we used one variable $n and one function fact() , By Setting “watch” option, dynamically you can monitor the varibles and function return value while debugging your code. Simply select the variable or function in your program , right click on it then select “New Watch” option. when debug the code bottom of the netbeans window will show the value of the variable.

Step 9: Finding Error

In this program , the the function return value is 0 if $n ==1. so it will always show the results as zero fro any input value.  change the return 0 statement to “return 1″ ,  now factorial program  will work perfectly.  The correct Program is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
/*
 * Author : Anthoniraj Amalan
 * Date   : 20 Sep, 2010
 */
function fact($n)
{
    if($n == 1 || $n == 0)
        return 1;
    else
       return $n * fact($n - 1);
}
echo "The factorial Value of 10 is ". fact(10);
?>

This is just a sample program to show how xdebug works with Netbeans, Xdebug would be really very useful in real time projects and also will save lot of time instead of finding and correct the error manually.

Leave a Reply