When you’re starting out with programming and reading tutorials as well as watching screencasts, it is sometimes hard to grasp what the code you are learning about is actually doing. Wouldn’t it be great to be able to go through the code step by step and watch the status of variables update in real time, cycle by cycle. Once you learn how to do this, you will be cooking with gas and ready to improve your knowledge radically.
We’re going to cover how to do this right now, it’s fun and easy! You’ll start by getting the Eclipse PDT all in one package by visiting the Zend website here http://www.zend.com/en/company/community/pdt/downloads and choosing your platform. In this case we’ll choose Windows.
Go ahead and download the package, unzip the contents and launch your Eclipse PDT software. Once you have downloaded and installed your software, you still have to install and configure a debugger in the IDE. There are a few ways to do this and it can be frustrating the first time you attempt it, but outlined here is the easiest method to install the debugger and get ready to step through some code. First you will navigate to
Window->Preferences->Install/Update->Available Software Sites.
Once there you will need to click Add, then put in the details as follows:
Now you will navigate to
Help->Install New Software and choose your newly added download location from the dropdown menu, then choose the Zend Debugger Feature, hit next and follow the prompts. Restart the IDE when asked.
We’re almost there and ready to start debugging and learning some snippets of code but first we need to configure our debug configurations. First create a new PHP project named ‘test’ by way of
File->New->Local PHP Project and create a simple index.php file in the project. Once you have done this add the following code snippet to your index.php file.
$vegibit = array(
"learn" => "PHP",
"read" => "vegibit.com",
Let’s now configure our debugger by choosing
Run->Debug Configurations. In this window you will choose PHP Script as a CLI Application, Alternate PHP for your installed PHPs and browse for the index.php file you just created in your project named ‘test’. In the name field we’ll simply call this instance of the debugger ‘debug it!’. Apply all settings once complete. Here is a screenshot of the settings.
Ok friends, let’s get debugging! Now that we have our project created, our debugger installed and configured, as well as the snippet of code provided saved in our index.php file, we can launch the debugger. There are several ways to do this in the IDE but we’ll choose the bug icon in the toolbar to launch this one. You will then be prompted for a PHP to Debug Perspective Switch, choose Yes.
This is where the fun starts as your code launches and stops at the very first line. We can now step into each and every line of code to debug exactly what it is doing. This gives us incredible insight into how our code works rather than the constant placement of print_r and var_dump statements in our PHP files. Below is what the IDE will look like as the code executes and stops at line 2. The IDE shows you all of the environment variables, console, and browser output, among many others.
Now click on the Step Into icon or F5 key two times slowly to watch a pre tag get echoed out for nice formatting, as well as see an array named $vegibit get created with the keys of learn and read as well as the values of PHP and Vegibit.com. Observe your newly created variable in the variables window in memory before these values are even sent to the screen! Pretty slick!
Finally click on Step Into one more time and finish the execution of the script to view the final output.
Now you have a debug environment that you can use to debug any snippet of PHP code you like. A great way to learn is to simply go to php.net and copy one of the many samples of code into your index.php file of your test project, save, and debug it the very same way we just did here. You can now step through any piece of code you can find and learn exactly what it does line by line in real time! Fun stuff indeed.
Thank you for reading Learn PHP and Programming by using the Eclipse PDT Debugger – If you found this post helpful, Please do share using the buttons below!