Debug PHP code in Netbeans

Netbeans is a powerful IDE for PHP and many other development environments. is it possible to debug your PHP project in Netbeans? run your PHP code line by line and look at your variable values in real time? The answer is Yes! even you can have many watch expressions to examine variable values and execute your PHP code line by line. You just need to follow these 4 steps to enable PHP debugging in your Netbeans IDE!

Note - Netbeans version 8.0.2 has used for this article

 

 

Step1: Set php.exe file location on your web server.

Click on Tools-> Options to open Options dialog box on Netbeans then go to PHP tab and set the php.exe file location (I am using xampp server, your path also will be similar to "F:\xampp\php\php.exe" if you use xampp, or if you use wamp server your path will similar to "F:\wamp\bin\php\php5.4.12\php.exe")








 

 

Step2: Setup debugging on Netbeans

Go to Debugging tab on Options dialogbox and set the values as you can see on following screenshot. Then click on Apply and OK to save your changes.

 

 

 

 

 

 

 

 

 

Step3: Setup php.ini file

Copy following lines to the end of your php.ini file.
Note - you can find this file on a similar location as  "F:\xampp\php\php.ini" if you are using xampp, or "F:\wamp\bin\php\php5.4.12\php.ini" if you are using wamp server.

[xdebug]
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 = "F:\xampp\tmp"
xdebug.remote_handler = dbgp
xdebug.remote_host = localhost
xdebug.remote_port = 9000

Note - you can change xdebug.profiler_output_dir = "F:\xampp\tmp" to match with your own folder

Step4: Restart your web server and Netbeans IDE to apply the changes that we made.



Now your IDE ready for debug your project. click on icon or Debug->Debug Project to start debugging on Netbeans. once you start debugging your browser will open the web page and you can put some break points on your code, then these break points will hit depends on your user actions on your web page. So you can examine your variable values and program execution using following icons on netbeans IDE. (or you can find the same actions on Debug menu on the IDE)

 
Continue Reading...

Style source codes on the web page

Is there any way to change style of the source code that you include in the web page with simple method? Yes, there is. If you read my previous posts such as hibernate operations, ant build file, you can see the way my source codes are presented. Key words, variable names, and class names, etc are formatted according to the language that source code belongs.

How did I formatted these text on the web page? Did I change font color of each words on the text? No there is a easy method to do that. Thanks to google code prettify on google code.

This is the method that you should follow to format your source code snippets in an html page.

You can simply download their zip file and add those source file to your web page by uploading to the hosting server.

But I will explain an easy way to do that using online resources itself without downloading and uploading.

Step 1
Okay first you have to add reference CSS and JavaScript file to your web page's head section. Copy following code to head section on your web page.

<link href='http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css' rel='stylesheet' type='text/css'/>
<script src='http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js' type='text/javascript'/>

If you are a google blogger you should copy the above code to blog's layout head section.
Here we use their online resource of CSS and JavaScript file to load styles and functions to our web page.

Step 2
Now just only one initialization for our code formatter. Copy the following code to body tag on your page.
You should use the layout as same as previous step, if you are a google blogger.

onload="prettyPrint()"

As an example

<body onload="prettyPrint()">

Step 3
Now just surround your text which you want to format with code or pre tag and set the Prettier class to specify the language. that is all. following example show you how to do that.

Example -
Normal text of your source code is,

//This is JAVA source code
import java.util.Scanner;
class keyboardDemo {
    public static void main(String[] arg) {
        //read from System.in = normally keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter two integers : ");
        int x;
        //read the inputs
        x=keyboard.nextInt();
    }
}

Now surround this text with code tag and particular style on your HTML source code.

<code class="prettyprint lang-java">
//This is JAVA source code<br />
import java.util.Scanner;<br />
class keyboardDemo {<br />
&nbsp;&nbsp; public static void main(String[] arg) {<br />
&nbsp;&nbsp; &nbsp;&nbsp; //read from System.in = normally keyboard<br />
&nbsp;&nbsp; &nbsp;&nbsp; Scanner keyboard = new Scanner(System.in);<br />
&nbsp;&nbsp; &nbsp;&nbsp; System.out.print("Enter two integers : ");<br />
&nbsp;&nbsp; &nbsp;&nbsp; int x;<br />
&nbsp;&nbsp; &nbsp;&nbsp; //read the inputs<br />
&nbsp;&nbsp; &nbsp;&nbsp; x=keyboard.nextInt();<br />
&nbsp;&nbsp; }<br />
}<br />
</code>

Now you can see the formatted source code as follows on your web page,

//This is JAVA source code
import java.util.Scanner;
class keyboardDemo {
    public static void main(String[] arg) {
        //read from System.in = normally keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter two integers : ");
        int x;
        //read the inputs
        x=keyboard.nextInt();
    }
}


If you need you can surround code with a div tag to change background color and wrapping style.

My div tag looks like this,

<div style="background: none repeat scroll 0% 0% rgb(209, 206, 206); border: 2px none; overflow: auto; white-space: nowrap;">

</div>

Now final styled text is,

//This is JAVA source code
import java.util.Scanner;
class keyboardDemo {
    public static void main(String[] arg) {
        //read from System.in = normally keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter two integers : ");
        int x;
        //read the inputs
        x=keyboard.nextInt();
    }
}

Code prettify support several language for style them. To change the language that you need to style just change the class of the style to your language.


For JAVA,

<code class="prettyprint lang-java">

Example -

//This is JAVA source code
import java.util.Scanner;
class keyboardDemo {
    public static void main(String[] arg) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter two integers : ");
        int x;
    }
}

For xml,

<code class="prettyprint lang-xml">

Example -

<project default="final" name="yourProjectName">
           <target name="final" description="this is a function to echo some text">
                    <echo message="Hello world..."/>
           </target>
</project>


For HTML,

<code class="prettyprint lang-html">

Example -

<!DOCTYPE html>
<html>
    <body>
        <h1>My First Heading</h1>
        <p>My first paragraph.</p>
    </body>
</html>

You can refer this page for further more support languages. So that's all have fun with this!



Continue Reading...