Debugging, for some people, is a part of the development process, but for others, it is a can of worms that should not be opened. Whatever your relationship with debugging, it is much easier to tackle with the right tools.
“Debugging is like being the detective in a crime movie where you are also the murderer,” — Filipe Fortes
Using the right tools can turn debugging into a form of art, rather than a losing battle. PHP programmers, for example, know there is a wide range of tools and options that will aid with the debugging process as long as they have a basic idea of how to use them.
Personally, I work mostly in PHP, so let’s start there.
Back to Basics
Firstly, let’s talk about debugging as a concept and forget for the moment any specific tools or code snippers
- The first and most important rule is to have some distance from your own code or the problem you are working on. Taking a break. 10 mins, 15 mins or even 30 mins if you have to (stretch your legs, put the coffee on…). Clear your mind, then tackle the problem with a fresh head.
- Talk to the duck. Rubber ducking is a problem-solving method that forces you to talk through your issue, instead of jumping straight into distracting someone else, and in the process, often discover the solution yourself.
- Ask for help. You are never alone in the dev world. Colleagues, friends, forums, or a really enthusiastic guy on YouTube. It is better to have a fresh pair of eyes to look at the problem if possible. In our office, we are lucky enough to have a team of experienced devs to ask, but if you are not as lucky as me, that’s what the internet is for.
Obviously, blindly copying and pasting a stranger’s code is not a great idea. Engage your common sense, and be wary of the source of the advice. Sites such as dev.to or stackoverflow.com are usually trustworthy enough, but even then this is not bullet-proof, you may need to do a bit of debugging. (Take a look at this recent study which found StackOverflow code to be bug-ridden.)
Simple PHP Commands
PHP provides a lot of ways to aid you with debugging, from something as simple as
echo(); commands to much more sophisticated tooling like
Let's talk about the basics of PHP, in the language, there are three (or four) commands to help devs dump data on the screen followed by a simple
exit(); statement (if we want to stop execution of the app after the print).
<?php$var0 = "Hi"; print($var0);
echo(); statements just print out almost anything we put as the argument. Sadly it does not handle array or object types variables too well. In case of arrays it outputs they give for non-primitive types isn’t very helpful (e.g. `Array` instead of printing the actual array contents). It will print out the content of the object only if implements
<?php$var0 = ;
$var0 = "Hi";print_r($var0);
 => Hi
print_r(); is just an enhanced version of the normal
echo(); commands which is also able to print objects or arrays out on to the screen.
$var0 = ;
$var0 = "Hi";var_dump($var0);
var_dump(); is a very special command. Instead of a normal print, it actually dumps the contents of the variable on to the screen including types, size, and content.
These basic tools that the developer can use to debug variables are the most common approach to debugging PHP. A simple print out of data is usually enough to determine what is happening as long as the developer has a good understanding of the code they are working with.
Printing Data for the Browser
Depending on the type of content of the print, a simple position of
<pre> tags around the print should help with rendering the code correctly. However, it does not cover every data structure and may have some issues with XML data, for example.
As a personal preference, I like to print my data in the pretty format. It prints out the data dump in a much more human-friendly way.
 => 0
[key] => value
[nested] => Array
[key] => 2
Those PHP tricks to just print out the data are usually enough to solve most of the problems/bugs but sometimes you have to use a power tool to fix a problem rather than sticking with simple hammer and nail technique…
The Power of Xdebug
…Xdebug is a powerful tool designed for PHP developers. It allows you to step line by line through the code while reading every single variable’s content. It is the most powerful tool a PHP dev can use to solve the problem and it does not require a lot to get it running.
The setup is very simple but varies depending on the type of operating system you’re using. These online tutorials can help with the setup. Once it’s setup just hook up the connection details to your preferred IDE (if possible) and start solving deeper problems.
I am not going to go into how to use XDebug as I would be here for the next month or so. All I will say that the tool is very user-friendly and any dev should be able to pick it up quickly. However, I will point out this article which helped me to learn how to use it - learning xdebug.
What Else Can Be Done?
Other than using the tools above (or equivalents), it is up to the original dev who designed the code. So please, when you write a function, class or just a simple algorithm, remember to leave a few comments around the code or write a simple
README file. It will help anyone who will have to debug the code and speed up the process. Code comments are not just for you, they are for anyone else who is going to read your code in the future.