Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Hide Buttons

How to debug php in Eclipse — Part1

In this basic tuto­r­ial we will dis­cuss about writ­ing our first hello world php pro­gram in eclipse with PDT (PHP Devel­op­ment Toolkit) plu­gin and than debug­ging it using the XDe­bug debug­ger. In the next sec­tion we will dis­cuss how to remote debug php run­ning within an Apache server.

Pre-requisites: This tuto­r­ial assumes that JDK and php are already installed on the computer.

Step1: Down­load Eclipse

We need to down­load eclipse from eclipse down­load site to start this tuto­r­ial. It can be down­loaded from eclipse site. To run php pro­grams within eclipse, the basic ver­sion of eclipse (Eclipse Stan­dard) should be fine, though we can down­load any eclipse ver­sion. Once the down­load is done, extract the eclipse and exe­cute the eclipse exe­cutable file located within the extracted eclipse direc­tory. We can also cre­ate the short­cut to avoid nav­i­gat­ing to the exe­cutable each time we want to run eclipse.

The eclipse will ask for a work­space loca­tion where it will store all the project related infor­ma­tion. A work­space is a group of related projects located at same place. We can select any loca­tion as our work­space loca­tion based on our requirements.

eclipse workspace dialog

It will launch eclipse and dis­play the wel­come dia­log. Once we close the dia­log the default work­space will be vis­i­ble. This is the work­space for Java devel­op­ment. Work­space is an arrange­ment of sev­eral pan­els and dialogs which helps us to develop our pro­grams. The code edi­tor is located at the cen­ter where we write all our code.

Step2: Install PDT (PHP Devel­op­ment Tools)

Since this tuto­r­ial is about writ­ing and debug­ging hello world for php, let’s install PDT into our eclipse.

Select Help->Install new Soft­ware and add the sites as listed below based on your eclipse version:

Juno — http://download.eclipse.org/releases/juno

Indigo — http://download.eclipse.org/releases/indigo

Helios — http://download.eclipse.org/releases/helios/

Once the list is pop­u­lated with updates, select PHP Devel­op­ment Tools (PDT) SDK Fea­ture and click Next. It will ask to accept the license agree­ment. Once we accept the agree­ment, it will install PDT and ask to restart eclipse at last. Once eclipse restart, the PDT select the php work­space and we are ready to start with php development.

Step3: Con­fig­ure Eclipse for php and XDebug

To check whether XDe­bug is installed we can use the XDe­bug link to iden­tify the cor­rect ver­sion required for our sys­tem. Paste the out­put of phpinfo() to the text area and click on Analyse my phpinfo() out­put but­ton. It will iden­tify the avail­able ver­sion of XDe­bug for cur­rent php instal­la­tion and also men­tion the instruc­tions to install it if it’s already not installed. It also ask to restart apache server once we have mod­i­fied zend_extension.

The XDe­bug instal­la­tion instruc­tions ask us to mod­ify the php.ini file for apache server. It will help us to debug our php scripts while run­ning the script using apache server. We will dis­cuss it in more details in the next post.

We need to mod­ify the php.ini file for php instal­la­tion, since we are going to debug our hello world for local php script.

Select Win­dows -> pref­er­ences -> PHP -> Debug. It should show two debug­gers i.e. Zend Debug­ger and XDe­bug within the PHP Debug­ger select box. We need to select XDebug.

php debugger eclipse

We also need to men­tion PHP exe­cutable for exe­cut­ing stand­alone PHP scripts. Click on PHP Exe­cuta­bles link and press Add but­ton to add php exe­cutable. By default will con­sider the php.ini file placed within the php instal­la­tion directory.

configure php executable

 

Now click on fin­ish and we are done with con­fig­ur­ing php debug­ger and exe­cutable to run php scripts within eclipse.

Step4: Cre­ate hello_world project

We need to switch over PHP per­spec­tive if it’s still not done by PDT instal­la­tion. To open php per­spec­tive, select Win­dow -> Open Per­spec­tive -> Other  and than select PHP and press ok. It should update the cur­rently active per­spec­tive at the top right cor­ner of eclipse win­dow high­light­ing the per­spec­tive PHP.

To cre­ate project within our work­space, select File -> New -> PHP Project and enter the project name as hello_world.

php project in eclipse

 

Press Fin­ish but­ton to cre­ate the project.

Step5: Cre­ate our first php file

First of all we will cre­ate a folder to store our php files. To cre­ate a folder, right click on project and select New -> Folder. The cre­ate folder wiz­ard will be dis­played sim­i­lar to the cre­ate project wiz­ard. Enter the folder name as hello and press Fin­ish but­ton. It will cre­ate a folder named hello within the hello_world php project.

To cre­ate our first php file, right click on the folder hello and select New -> PHP File. Cre­ate file wiz­ard will be dis­played. It shows the Source Folder and pro­vide options to change it. Enter the php File Name as hello_world.php and press Fin­ish. It will cre­ate the php file within the folder hello

hello world php eclipse

Step6: Write code and exe­cute php script

Now we will add some code for exe­cu­tion as men­tioned below to print hello world on console.

echo "Hello World";

To exe­cute the script right click on hello_world.php and select Run As -> Run Con­fig­u­ra­tions and dou­ble click on PHP CLI Appli­ca­tion from the list avail­able on right panel within the Run Con­fig­u­ra­tions dia­log. It will add a new con­fig­u­ra­tion to exe­cute our php script.

php eclipse run configuration

Enter the Run Con­fig­u­ra­tion name and browse for the php file as shown above. Now press Apply but­ton and than press Run. It’s done. We can see the out­put on eclipse console.

To re-run the script, we need to press the high­lighted green but­ton as shown above.

Step7: Debug the php script

Debug­ging php script in eclipse is very easy. To debug the script, first of all we need to add break-point for the state­ment we need to debug. While debug­ging the exe­cu­tion stops at the break points added by us and than we can exe­cute the state­ments line by line check­ing the out­put of each state­ment. To add a break­point, we need to dou­ble click against the state­ment we wish to debug.

Let’s write the below men­tioned code and we will add break­point to check vari­able values.

$num1 = 10;
$num2 = 20;

$sum = $num1 + $num2;

echo "Sum:" . $sum;

 

To debug the php file, we need to press the select arrow but­ton next to the bug icon and than press on our run con­fig­u­ra­tion hello_world. It will start the debug ses­sion and ask to switch to debug per­spec­tive. Press Yes and eclipse will open the debug per­spec­tive as shown below.

The debug­ger will stop on first state­ment and wait for our next com­mand. We can see the debug­ger arrow on line 3. The debug per­spec­tive also show the asso­ci­ated vari­ables, break­points and debug­ger thread. To pro­ceed ahead, we nor­mally use the com­mands as listed below:

  1. Step Into — To exe­cute cur­rent state­ment and move the debug­ger to next statement.
  2. Step Over — To jump the cur­rent state­ment. If the cur­rent state­ment is a func­tion call, it will just exe­cute the func­tion and debug­ger won’t enter into function.
  3. Con­tinue — The con­tinue com­mand move the debug­ger to next break point.
  4. Ter­mi­nate — Ter­mi­nate com­mand ter­mi­nates the debugger.

debug commands ecplise

 

 

 

 

 

 

These 4 com­mands are quite handy while debug­ging a php pro­gram. The vari­able val­ues will be updated as we keep on exe­cut­ing our state­ments using the debug commands.

These are the basic con­cepts about how we can cre­ate php project and debug it using eclipse and XDebug.

 

 

9 Responses

  1. Alain Trust says:

    Thank you very much for this tuto­r­ial. I spent almost a whole day try­ing to con­fig­ure and use XDe­bug to debug PHP in Eclipse until I find this won­der­ful tuto­r­ial. This really helps accom­plish my goals. Again, I really appre­ci­ate all of your efforts in putting together such a great tuto­r­ial. You are service!!!

  2. JOJO says:

    This is a great Tuto­r­ial it help me a lot

    With Regards

  3. Phuong says:

    This tuto­r­ial won­der­fully help and save my time. Thanks a lot.

    Regards,

  4. keluoibieng says:

    it’s very easy to fol­low this. thanks for your sharing

  5. Sarath says:

    Thanks for post­ing. This arti­cle helps me a lot. But i was still fac­ing an issue. The break points which i kept in action page are not work­ing. Only the break points which are in view and js pages are only work­ing. Can any one please help me out…??

  6. emilio says:

    This Tuto­r­ial is very well writ­ten, really easy to under­stand, ad saved me a lot of time. Thank you very much for shar­ing it with us.
    Emilio

  7. Zachary says:

    I nor­mally pro­gram in java, so when I was requested to build a PHP web project I had no idea how to use STS with PHP. At first it was not a huge issue but as the app got more com­pli­cated debug­ging with var dumps and echos became a has­sle and I was annoyed at the fact I could not step through the code. This tuto­r­ial saved my life and my hair. many thanks.

  8. Peter says:

    Thank you very much! Your are my helper 😉 Many many thanks!

  9. Praveen says:

    Thank you !! Arti­cle is elab­o­rate and quite helpful.

Leave a Reply

Your email address will not be published. Required fields are marked *