Debugging in PHP with Xdebug and PHPStorm

Debugging is the process of identifying and correcting programming errors. It is known in English as debugging, which is similar to the elimination of bugs, the informal name for programming errors. The term bug is said to come from the era of  thermionic valve computers , in which problems were generated by insects that were attracted to lights and damaged the equipment.

In this article we will see how to find “bugs” in our PHP code and with the arrival of Drupal 8 I would highly recommend its use, due to the amount of data that the objects themselves carry, and that by doing a var_dump() it would be almost impossible to understand in the browser.

1. Instalar Xdebug:

First we will have to create a file, I will put phpinfo.php with the following code:

We locate it in the root of our server and access it in our browser, in my case it would be:  http://localhost/phpinfo.php

We would see something like this:

phpinfo

We mark all the content and copy it:

phpinfo_copy

We go to  http://xdebug.org/wizard.php  where we paste it and click on the “Analyse my phpinfo() output” button:

xdebug

Below the site will show you the steps to install Xdebug on your Operating System.

xdebug_instructions

In the step where the extension is activated in your php.ini file, also add the following lines:

xdebug.remote_enable=1

xdebug.remote_host=127.0.0.1

xdebug.remote_port=9000

xdebug.idekey=PHPSTORM

xdebug.max_nesting_level=500

As shown in the following image:

xdebug_config

 

2. Add the PHPStorm bookmarklet to our browser:

We need to go to  http://www.jetbrains.com/phpstorm/marklets/  and click on the “Generate” button

 

xdebug_bookmarket

 

Below we will have the links available:

bookmarket_generated

 

We should add them to our favorites, because we will use them every time we go to debug, just drag it to our favorites bar

firefox_bookmarkets

 

3. Let's try it!

We navigate to a page made in PHP. We add a breakpoint in PHPStorm by left-clicking on the column between the line numbers and the code:

PHPStorm breakpoint

We press the button that looks like a telephone

PHPStorm telephone

O en el menú Run → Start Listening for PHP Debug Connections

PHPStorm menu telephone

Let’s go to the browser. Click on our “Start Debugger” bookmark and refresh the page.

Firefox start debug

PHPStorm should display a window, asking for permission to start debugging:

PHPStorm incoming connection

By accepting, we can begin to execute our code step by step.

Debugging tools window:

PHPStorm toolbox

  1. Debugging toolbar , in which we can skip all breakpoints, stop or continue the execution of our code.

  2. In the Frames panel , we will have access to the successive calls that have been made until reaching our breakpoint.

  3. The Variables panel shows all the variables available in our scope up to where our application is stopped.

  4. The Watches panel , here you can include your most important variables and monitor them.

  5. The Console tab will display system information, error messages, and data input and output for our application.

To debug our code, we have the following options:

PHPStorm show execution point  (Show Execution Point) Clicking on this button shows us at what point our application is stopped.

PHPStorm step over(Step Over) By clicking this button we can debug line by line.

PHPStorm step into(Step Into) Clicking this button causes us to debug inside the functions we call. For example:

PHPStorm function into

By doing Step into in the previous line we can debug inside the square function and in the sum function. If there are several functions, as is the case and we only want to enter a specific one, we must use Run → Smart Step Into:

PHPStorm smart step into

Here we specify that we want to enter only the sum function.

PHPStorm force step into (Force Step Into) By clicking on this button we force the debugger to enter functions that normally the “Step Into” would not enter, such as External Libraries, Constructors, etc.

PHPStorm step out(Step Out) Clicking this button causes the debugger to exit the current function and move to the line that follows before it was called.

PHPStorm run to cursor(Run to Cursor) When you click this button, the debugging program advances to the cursor, this occurs without the need to use a breakpoint.

PHPStorm evaluate expression  (Evaluate Expression) Clicking this button can calculate the value of an expression while debugging.

As you can see in our code we use a foreach, you may wonder how to evaluate a variable only when a certain condition is met, for example when $number == 7, normally we should use “Step over” again and again and stop to be evaluated when said condition is met. PHPStorm has a feature called conditional breakpoints, which when a condition is met stops the execution of the application. To create one, just right click on one already created and add our condition:

PHPStorm breakpoint conditionals

 

So when the condition is met, the script execution will stop at that point.

Well, I hope this article has been useful to you and helps you develop and be more productive in PHP.