How to debug php in Eclipse – Part1
In this basic tutorial we will discuss about writing our first hello world php program in eclipse with PDT (PHP Development Toolkit) plugin and than debugging it using the XDebug debugger. In the next section we will discuss how to remote debug php running within an Apache server.
Pre-requisites: This tutorial assumes that JDK and php are already installed on the computer.
Step1: Download Eclipse
We need to download eclipse from eclipse download site to start this tutorial. It can be downloaded from eclipse site. To run php programs within eclipse, the basic version of eclipse (Eclipse Standard) should be fine, though we can download any eclipse version. Once the download is done, extract the eclipse and execute the eclipse executable file located within the extracted eclipse directory. We can also create the shortcut to avoid navigating to the executable each time we want to run eclipse.
The eclipse will ask for a workspace location where it will store all the project related information. A workspace is a group of related projects located at same place. We can select any location as our workspace location based on our requirements.
It will launch eclipse and display the welcome dialog. Once we close the dialog the default workspace will be visible. This is the workspace for Java development. Workspace is an arrangement of several panels and dialogs which helps us to develop our programs. The code editor is located at the center where we write all our code.
Step2: Install PDT (PHP Development Tools)
Since this tutorial is about writing and debugging hello world for php, let’s install PDT into our eclipse.
Select Help->Install new Software 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 populated with updates, select PHP Development Tools (PDT) SDK Feature and click Next. It will ask to accept the license agreement. Once we accept the agreement, it will install PDT and ask to restart eclipse at last. Once eclipse restart, the PDT select the php workspace and we are ready to start with php development.
Step3: Configure Eclipse for php and XDebug
To check whether XDebug is installed we can use the XDebug link to identify the correct version required for our system. Paste the output of phpinfo() to the text area and click on Analyse my phpinfo() output button. It will identify the available version of XDebug for current php installation and also mention the instructions to install it if it’s already not installed. It also ask to restart apache server once we have modified zend_extension.
The XDebug installation instructions ask us to modify the php.ini file for apache server. It will help us to debug our php scripts while running the script using apache server. We will discuss it in more details in the next post.
We need to modify the php.ini file for php installation, since we are going to debug our hello world for local php script.
Select Windows -> preferences -> PHP -> Debug. It should show two debuggers i.e. Zend Debugger and XDebug within the PHP Debugger select box. We need to select XDebug.
We also need to mention PHP executable for executing standalone PHP scripts. Click on PHP Executables link and press Add button to add php executable. By default will consider the php.ini file placed within the php installation directory.
Now click on finish and we are done with configuring php debugger and executable to run php scripts within eclipse.
Step4: Create hello_world project
We need to switch over PHP perspective if it’s still not done by PDT installation. To open php perspective, select Window -> Open Perspective -> Other and than select PHP and press ok. It should update the currently active perspective at the top right corner of eclipse window highlighting the perspective PHP.
To create project within our workspace, select File -> New -> PHP Project and enter the project name as hello_world.
Press Finish button to create the project.
Step5: Create our first php file
First of all we will create a folder to store our php files. To create a folder, right click on project and select New -> Folder. The create folder wizard will be displayed similar to the create project wizard. Enter the folder name as hello and press Finish button. It will create a folder named hello within the hello_world php project.
To create our first php file, right click on the folder hello and select New -> PHP File. Create file wizard will be displayed. It shows the Source Folder and provide options to change it. Enter the php File Name as hello_world.php and press Finish. It will create the php file within the folder hello
Step6: Write code and execute php script
Now we will add some code for execution as mentioned below to print hello world on console.
echo "Hello World";
To execute the script right click on hello_world.php and select Run As -> Run Configurations and double click on PHP CLI Application from the list available on right panel within the Run Configurations dialog. It will add a new configuration to execute our php script.
Enter the Run Configuration name and browse for the php file as shown above. Now press Apply button and than press Run. It’s done. We can see the output on eclipse console.
To re-run the script, we need to press the highlighted green button as shown above.
Step7: Debug the php script
Debugging php script in eclipse is very easy. To debug the script, first of all we need to add break-point for the statement we need to debug. While debugging the execution stops at the break points added by us and than we can execute the statements line by line checking the output of each statement. To add a breakpoint, we need to double click against the statement we wish to debug.
Let’s write the below mentioned code and we will add breakpoint to check variable values.
$num1 = 10; $num2 = 20; $sum = $num1 + $num2; echo "Sum:" . $sum;
To debug the php file, we need to press the select arrow button next to the bug icon and than press on our run configuration hello_world. It will start the debug session and ask to switch to debug perspective. Press Yes and eclipse will open the debug perspective as shown below.
The debugger will stop on first statement and wait for our next command. We can see the debugger arrow on line 3. The debug perspective also show the associated variables, breakpoints and debugger thread. To proceed ahead, we normally use the commands as listed below:
- Step Into – To execute current statement and move the debugger to next statement.
- Step Over – To jump the current statement. If the current statement is a function call, it will just execute the function and debugger won’t enter into function.
- Continue – The continue command move the debugger to next break point.
- Terminate – Terminate command terminates the debugger.
These 4 commands are quite handy while debugging a php program. The variable values will be updated as we keep on executing our statements using the debug commands.
These are the basic concepts about how we can create php project and debug it using eclipse and XDebug.