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

We have discussed how to write a hello world php application and debug php using Aptana Studio in the previous tutorials I wrote for windows platform – Part 1 and Part 2. In this tutorial we will discuss how we can write a hello world php application and than debug it using Aptana Studio for linux. I have used Ubuntu 14.04 LTS for demonstration purpose, but the steps will be similar for other linux flavors.

This tutorial assumes that we are already familiar 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 Xdebug for PHP, we need to give the below mentioned command on Terminal. It will install Xdebug for PHP if it’s not done previously.

~$ apt-get install php5-xdebug

To check whether it’s installed correctly, we can use locate command as shown below. It will print the location where xdebug is installed.

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

Now we need to configure Xdebug for PHP. It can done by modifying the php.ini file. We can check the php.ini file location using the locate command. This tutorial will be using the ini file present within the cli directory.

Our next step is to modify php.ini file present within the cli directory so that Xdebug module is enabled for debugging 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 XDebug module. That’s it. Now we are ready to debug our php scripts using Aptana Studio.

Step 2 – Install Aptana Studio

To install Aptana Studio for Ubuntu, we need to download it from – Download Aptana Studio. Once download is done, we can extract the downloaded zip to any location of our choice. To add a desktop shortcut with Aptana Studio Icon, we need to follow below mentioned steps:

The first command install GNOME panel and the second command help us to add a desktop shortcut. When we run second command, it open up a window which ask for icon location and the executable file. We need to do 3 things to setup a desktop launcher:

1. To change the icon, click on the default icon with spring symbol and browse for the location where we have extracted Aptana Studio. We need to choose the icon file having name icon.xpm.
2. Enter Application Name. In our case, it could be Aptana Studio.
3. To set the executable, we need to click on Browse Button and choose the executable file AptanaStudio3 from the location where we have extracted Aptana Studio

Step 3 – Create Hello World Project
After installing Aptana Studio, we need to create our first PHP Project. To create a new PHP Project, launch aptana studio and choose a workspace where we want to save all our project files. While choosing workspace, we should not check the Use this as default and do not ask again Checkbox else it won’t ask us to choose our workspace while launching Aptana next time. If we did it accidentally, we need not worry since we can change this setting later.

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

The above mentioned screenshots shows how we can create a new PHP Project using Aptana Studio.

Step 4 – Write code for debugging purpose
In this step we will create the PHP script we are going to debug using Aptana Studio. To create a new PHP script, we need to right click on the PHP Project and than click on New From Template -> PHP -> PHP Template. It will open up the File Creation wizard as shown below.

We need to enter the name of our PHP script and than press Finish Button. It will create a new php file having the default code as shown below:

phpinfo();

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

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

Step 5 - Configure Xdebug for Project

We need to configure Aptana Studio for the installed PHP and the ini file. To configure PHP, click on Windows -> Preferences. It will open the preferences Dialog for Aptana Studio where we can do several type of configuration. To configure PHP Interpreter, we need to click on Aptana Studio -> Editors -> PHP -> PHP Interpreters. It will show the installed PHP for Aptana Studio. Initially it won't show anything since Aptana do not configure any interpreter by default.

To add a new Interpreter, we need to click on Add Button. It will open the wizard which allows us to add the PHP executable and configure the ini file we have edited in previous steps as shown below.

Once we have configured the PHP interpreter, we need to click on Finish Button. We need to make sure that the Xdebug is set for PHP Debugger while configuring interpreter. It will list our Interpreter as shown below.

Now click on OK Button and we are done with configuring PHP and Xdebug to debug our scripts.

Step 6 - Debug PHP scripts

To debug our script we wrote in previous steps, we need to understand few basic concepts of debugging.

1. Debugger - The debugger executes our program statements one by one.
2. Breakpoint - We can pause the debugger execution for the selected statements. It can be done by specifying breakpoint for these statement. When debugger is paused at a breakpoints, we can analyse our code and check what variables are initialised so far and their present value. This is very helpful in debugging our code.
3. Resume - We can resume the debugger paused at a particular breakpoint and it starts executing the next statements.
4. Terminate - We can terminate the Debugger at any point. Usually we do it once we found bug in our code.
5. Step Into - We can Step Into the breakpoint and allows debugger to further execute the statements. This is very much useful when we want our debugger to execute the statements for a particular method.
6. Step Over - The Step Over can be used to skip the current breakpoint.

To start debugging, we will add our first breakpoint. To add a breakpoint, we need to double click before the line number on the code editor. It will show a blue bullet which confirm that our breakpoint is added successfully as shown below.

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

The debugger will start executing our script statements and pause at the first line as shown below.

To continue debugging, we need to click on Continue Button. The debugger will stop at the breakpoint we have added on the echo statement. At this point, we can analyse our script and check that the value of variable i is set to 0 on the variables stack. On left hand side, we can also check how many breakpoints are configured for our script by clicking on Breakpoints Tab.

Now press on Step Into and the debugger will move to next statement. Each time we press Step Into, the debugger executes next statement and update the variables stack. We can press Continue Button any time and debugger will search for next Breakpoint. If it does not find any, it exits execution.

This is how we can debug our php scripts. We can add breakpoints where we suspect bug and than execute the statements by pressing Step Into or Step Over.