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

How to debug php in Aptana Studio for linux — Part 1

We have dis­cussed how to write a hello world php appli­ca­tion and debug php using Aptana Stu­dio in the pre­vi­ous tuto­ri­als I wrote for win­dows plat­form — Part 1 and Part 2. In this tuto­r­ial we will dis­cuss how we can write a hello world php appli­ca­tion and than debug it using Aptana Stu­dio for linux. I have used Ubuntu 14.04 LTS for demon­stra­tion pur­pose, but the steps will be sim­i­lar for other linux flavors.

This tuto­r­ial assumes that we are already famil­iar with Ubuntu and PHP and JDK are already installed. It also assumes that we know the basics of PHP.

Step 1 — Install Xdebug

To install Xde­bug for PHP, we need to give the below men­tioned com­mand on Ter­mi­nal. It will install Xde­bug for PHP if it’s not done previously.

~$ apt-get install php5-xdebug

To check whether it’s installed cor­rectly, we can use locate com­mand as shown below. It will print the loca­tion where xde­bug is installed.

~$ locate xdebug
~$ /usr/lib/php5/20121212/xdebug.so

Now we need to con­fig­ure Xde­bug for PHP. It can done by mod­i­fy­ing the php.ini file. We can check the php.ini file loca­tion using the locate com­mand. This tuto­r­ial will be using the ini file present within the cli directory.

~$ locate php.ini
~$ /etc/php5/apache2/php.ini
~$ /etc/php5/cli/php.ini
~$ /usr/share/php5/php.ini-development
~$ /usr/share/php5/php.ini-production
~$ /usr/share/php5/php.ini-production.cli

The below men­tioned com­mand test whether php5 is installed on our sys­tem. If php is installed prop­erly, it prints the cur­rent php version.

~$ php5 --version
PHP 5.5.9-1ubuntu4.4 (cli) (built: Sep  4 2014 06:56:34) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies
    with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans

Our next step is to mod­ify php.ini file present within the cli direc­tory so that Xde­bug mod­ule is enabled for debug­ging purpose.

~$ nano /etc/php5/cli/php.ini

We need to add the line zend_extension=”/usr/lib/php5/20121212/xdebug.so” at the last of php.ini file to enable the XDe­bug mod­ule. That’s it. Now we are ready to debug our php scripts using Aptana Studio.

Step 2 — Install Aptana Studio

To install Aptana Stu­dio for Ubuntu, we need to down­load it from — Down­load Aptana Stu­dio. Once down­load is done, we can extract the down­loaded zip to any loca­tion of our choice. To add a desk­top short­cut with Aptana Stu­dio Icon, we need to fol­low below men­tioned steps:

~$ apt-get install --no-install-recommends gnome-panel
~$ gnome-desktop-item-edit ~/Desktop/ --create-new

The first com­mand install GNOME panel and the sec­ond com­mand help us to add a desk­top short­cut. When we run sec­ond com­mand, it open up a win­dow which ask for icon loca­tion and the exe­cutable file. We need to do 3 things to setup a desk­top launcher:

1. To change the icon, click on the default icon with spring sym­bol and browse for the loca­tion where we have extracted Aptana Stu­dio. We need to choose the icon file hav­ing name icon.xpm.
2. Enter Appli­ca­tion Name. In our case, it could be Aptana Stu­dio.
3. To set the exe­cutable, we need to click on Browse But­ton and choose the exe­cutable file AptanaStudio3 from the loca­tion where we have extracted Aptana Studio

php-debug-1

Step 3 — Cre­ate Hello World Project
After installing Aptana Stu­dio, we need to cre­ate our first PHP Project. To cre­ate a new PHP Project, launch aptana stu­dio and choose a work­space where we want to save all our project files. While choos­ing work­space, we should not check the Use this as default and do not ask again Check­box else it won’t ask us to choose our work­space while launch­ing Aptana next time. If we did it acci­den­tally, we need not worry since we can change this set­ting later.

php-debug-2

Next, we need to click on File -> New -> PHP Project.

php-debug-3

php-debug-4

The above men­tioned screen­shots shows how we can cre­ate a new PHP Project using Aptana Studio.

Step 4 — Write code for debug­ging pur­pose
In this step we will cre­ate the PHP script we are going to debug using Aptana Stu­dio. To cre­ate a new PHP script, we need to right click on the PHP Project and than click on New From Tem­plate -> PHP -> PHP Tem­plate. It will open up the File Cre­ation wiz­ard as shown below.

php-debug-5

We need to enter the name of our PHP script and than press Fin­ish But­ton. It will cre­ate a new php file hav­ing the default code as shown below:

phpinfo();

Now we will write a for loop which prints Hello Aptana Stu­dio ! 5 times and than save the file.

for( $num = 0; $num < 5; $num++ ) {
    	
	echo "Hello Aptana !";
}

Step 5 — Con­fig­ure Xde­bug for Project

We need to con­fig­ure Aptana Stu­dio for the installed PHP and the ini file. To con­fig­ure PHP, click on Win­dows -> Pref­er­ences. It will open the pref­er­ences Dia­log for Aptana Stu­dio where we can do sev­eral type of con­fig­u­ra­tion. To con­fig­ure PHP Inter­preter, we need to click on Aptana Stu­dio -> Edi­tors -> PHP -> PHP Inter­preters. It will show the installed PHP for Aptana Stu­dio. Ini­tially it won’t show any­thing since Aptana do not con­fig­ure any inter­preter by default.

php-debug-6

To add a new Inter­preter, we need to click on Add But­ton. It will open the wiz­ard which allows us to add the PHP exe­cutable and con­fig­ure the ini file we have edited in pre­vi­ous steps as shown below.

php-debug-7

Once we have con­fig­ured the PHP inter­preter, we need to click on Fin­ish But­ton. We need to make sure that the Xde­bug is set for PHP Debug­ger while con­fig­ur­ing inter­preter. It will list our Inter­preter as shown below.

php-debug-8

Now click on OK But­ton and we are done with con­fig­ur­ing PHP and Xde­bug to debug our scripts.

Step 6 — Debug PHP scripts

To debug our script we wrote in pre­vi­ous steps, we need to under­stand few basic con­cepts of debugging.

1. Debug­ger — The debug­ger exe­cutes our pro­gram state­ments one by one.
2. Break­point — We can pause the debug­ger exe­cu­tion for the selected state­ments. It can be done by spec­i­fy­ing break­point for these state­ment. When debug­ger is paused at a break­points, we can analyse our code and check what vari­ables are ini­tialised so far and their present value. This is very help­ful in debug­ging our code.
3. Resume — We can resume the debug­ger paused at a par­tic­u­lar break­point and it starts exe­cut­ing the next state­ments.
4. Ter­mi­nate — We can ter­mi­nate the Debug­ger at any point. Usu­ally we do it once we found bug in our code.
5. Step Into — We can Step Into the break­point and allows debug­ger to fur­ther exe­cute the state­ments. This is very much use­ful when we want our debug­ger to exe­cute the state­ments for a par­tic­u­lar method.
6. Step Over — The Step Over can be used to skip the cur­rent breakpoint.

php-debug-12

To start debug­ging, we will add our first break­point. To add a break­point, we need to dou­ble click before the line num­ber on the code edi­tor. It will show a blue bul­let which con­firm that our break­point is added suc­cess­fully as shown below.

php-debug-9

Now right click on the php script hello_aptana.php and click on Debug As -> PHP Script. It will ask to switch to Debug Per­spec­tive as shown below.

php-debug-10

The debug­ger will start exe­cut­ing our script state­ments and pause at the first line as shown below.

php-debug-11

To con­tinue debug­ging, we need to click on Con­tinue But­ton. The debug­ger will stop at the break­point we have added on the echo state­ment. At this point, we can analyse our script and check that the value of vari­able i is set to 0 on the vari­ables stack. On left hand side, we can also check how many break­points are con­fig­ured for our script by click­ing on Break­points Tab.

php-debug-13

Now press on Step Into and the debug­ger will move to next state­ment. Each time we press Step Into, the debug­ger exe­cutes next state­ment and update the vari­ables stack. We can press Con­tinue But­ton any time and debug­ger will search for next Break­point. If it does not find any, it exits execution.

This is how we can debug our php scripts. We can add break­points where we sus­pect bug and than exe­cute the state­ments by press­ing Step Into or Step Over.

You may also like...

1 Response

Leave a Reply

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