📜 ⬆️ ⬇️

Debugging with XDebug and PhpStorm using the example of 1C-Bitrix site

In this article, I will tell you how convenient it is to debug code in PhpStorm using the example of working with a website running in the 1C-Bitrix system. We will work with IDE PhpStorm and XDebug debugger. I assume that XDebug is already installed on your server and you have already created a project in PhpStorm.

In this article, examples are given with the following set of tools:


So, let's begin.

')
During development, you often have to use functions like print_r () or echo to display data on the screen. I personally, until recently, used functions like:

function pre($array) { echo '<pre>'; print_r($array); echo "</pre>"; } 


This function printed an array on the screen and already in this form, I disassembled it and made edits to the code. But what to do if the print of a huge $ arResult on the page is invalid? What if there are already visitors on the site and it is categorically impossible to display system information ( an article will be useful for remote debag)? Then there are even more interesting functions like:

 function pre2($array) { global $USER; if($USER->isAdmin()) { echo '<pre>'; print_r($array); echo '</pre>'; } } 


Such a function displays prints on the screen only for site administrators, which partially solved the problem, but the usability of such a solution leaves much to be desired.

XDebug and PhpStorm


These two products give us the opportunity to abandon the above functions and indeed output variables to the screen when debugging the application and receive data directly in the IDE in a convenient and readable form:

image

In the screenshot above, we can see the $ APPLICATION object in the debug window of the /index.php file. We received complete information about the object, in a grouped form, while we can observe the data type of each element in the object. And (attention!) We didn’t write any var_dump () or print_r () anywhere!

Setting up PhpStorm for debugging with XDebug


Before you start to use this beauty, we need to perform several actions:

1. Configure XDebug on the server

First we need to make sure that XDebug is installed on the server. In this article I will not consider the installation of XDebug on the server, there are many articles on the Internet about this. I’ll just show you what you had to do in MAMP to activate XDebug :

All actions were performed with a php.ini file for building PHP version 5.5.3. How to get to this file in MAMP - below in the screenshot

image

In this file you need to unwrap the line
zend_extension=»/Applications/MAMP/bin/php/php5.5.3/lib/php/extensions/no-debug-non-zts-20121212/xdebug.so»

and add a line
xdebug.remote_enable=1

image

After that, phpinfo () will give us the following information:

image

XDebug is ready to go.

2. Configure PhpStorm

To get data from the site in the IDE, we need to befriend them. First you need a properly configured server to which the IDE connects to the deployment. Go to the settings PhpStorm -> Deployment . The Webserver Root URL parameter is important here, you need to specify the URL of the root of your site. An example of my settings below:

image

Next, go to the XDebug configuration in PhpStorm : main menu Run -> Edit Configurations

image

Click on the plus to add a new setting for debug.

image

Choose the setting of Php Web Application

image

Here you need to configure the tool with the help of which the debug is performed, click on the button next to the server setup:

image

Click on the plus, fill in the name, host the site and click on Validate remote environment .

image

There we select from the list the server that was configured above and click Validate

image

If the validation was successful, then we are ready for debug. Apply all saved settings and return to the editor.

3. Breakpoint arrangement (breakpoints)

To find out the values ​​of variables in the current code, you need to tell our IDE exactly where in the code we want to get the values ​​we need. For this, the breakpoint functionality is provided. Select the desired line of the php-code and click on the area to the right of the line number.

image

4. Launch debug

Next, you need to enable listening to the 9000 port, to which XDebug will send a response when the script starts, and click on the green bug on the left (the button locations in the windows version of PhpStorm may differ).

image

After clicking on the beetle, our browser should open

image

and after a few seconds, PhpStorm will open again with a debugging panel, where the values ​​of all variables will be at the time of processing the breakpoint location code

image

Source: https://habr.com/ru/post/209024/


All Articles