Debug PHP applications

Volodymyr Ustinov
5 min readNov 22, 2022

Hello everyone! Today I am going to talk about debugging in PHP applications, but first let’s try to understand “Debugging”. Debugging, Wikipedia says, is the process of finding and resolving bugs. To find bugs we need to somehow test how our code works, and for this we need some tools. Most PHP developers know about such internal tools for investigating code like functions echo, print_r, var_dump, debug_backtrace and some others. Also we can try setup logs and try to find some info there.
With usage of an internal toll like function var_dump, which displays info about variables we can face a problem like very big amounts of info is displayed, especially when you put inside function var_dump some object, for example object from Doctrine ORM. In this case we can see something like that:

not really useful, dont it? But we can improve this output with help of package like symfony/var-dumper, so same result of dump Doctrine object with usage this package will looks like this:

I think this is much easier to read and understand. And if you will put some big object inside var_dump() function you can faced with problem out of memory and long time of print this in your browser. So tools like Symfony dump() really help with this.

Tools like var_dump, Symfony dump() and others like that are good when you need to debug something fast and simple. There are some cons like you need to put some additional code to do this debugging, you often can forget about it and at some point your customers can see some strange output. Fortunately, we have tools like Xdebug, as described on the site of profiler Xdebug provide to you “A way to step through your code in your IDE or editor while the script is executing.” And this is much smarter, efficient and simple to use, read and understand. And it has good documentation.

Before using it, we need to install a tool. And there are few possible ways. For example if you are using php in your Ubuntu machine you can try this one:
sudo apt-get install php-xdebug. I had test it with Docker, so my case looks like this:

RUN pecl install xdebug in Dockerfile. And we need provide settings for Xdebug, i did it like that:

And whole Dockerfile looks like that:

Xdebug provides some improvement for the internal php var_dump() function. After you had install Xdebug you output of var_dump will look something like that:

This is really similar to the Symfony dump() function.

And also Xdebug give us a pretty view of errors, it looks something like this:

for enable this view and pretty view of var_dump function you need to install Xdebug and set in php.ini xdebug.mode = develop

Actually Xdebug does overload internal var_dump to his own function.
For make Xdebug be available for step by step debugging we need set mode like this: xdebug.mode = debug

For step by step debugging we need to set up a Php storm IDE regarding settings in php.
In general preferences we need find screen of PHP > Debug and set port regarding settings in php for

As I am using Docker, for PhpStorm this looks like remote debug, so we need to set up remote debug. To do this we need to go to Run > Edit Configurations, there we need click at plus and in dropdown select PHP Remote Debug. After we should see screen like this:

in section Server we should click on three dots for add our server, it looks like this:

The important thing is to set your remote directory, in my case from inside of the container to your local directory. After that you should be able to do remote debugging step by step. To do this we should put somewhere in code breakpoint, click on button “Start Listening” and run you code, for me result looks like that:

now you can do step into, step over… for navigate in your code in debug mode. This gives you the possibility to check what is going inside your code without any additional functions like echo, print_r, var_dump, debug.

Example before cover debug on local machine, as I mentioned we should put in settings for Xdebug remote xdebug.client_host=host.docker.imternal. This allows a docker link container with your local machine. But we can tune this a bit for debug in real remote mode. To do this we need to somehow link our remote host machine and local machine with IDE. We can use such tools like ngrok. It helps make tunnels between our machines. So to make a tunnel we need to make a Ngrok tcp tunnel like this: ngrok tcp 9005, port should be the same as you set in PHP Storm. Result of command should looks like that:

As a result we can see we got tunnel and now we can use 0.tcp.ngrok.io and port 18262 and update our xdebug.ini regarding this host and port. I know this is not really comfortable to do so for remote debug, but this is just an example of remote debugging, and also you can try to get a paid Ngrok account and I think your host should be the same every time you run a tunnel.

--

--

Volodymyr Ustinov

Hi, I’m a software engineer. My career started at 2012. My common ecosystem is backend with PHP