Debugging PHP, Drupal and MySQL/MariaDB.

Debugging PHP, Drupal and MySQL/MariaDB.


In the series of articles, I will try to describe and systematize the process of debugging PHP, Drupal and MySQL / MariaDBas much as possible. I will speak about ways of debugging, special software (such as IDE), and setting. We will also touch on separately debug queries to the database as MySQL / MariaDB.

Modern debuggers allow stopping the execution at a specific line (stopping points), passing the call stack, displaying the values ​​of variables in local and global contexts, collect profile information about the speed, memory consumption.

Without Debugging usually any major project does not work. Doesn`t matter if site is on Yii Framework, or Drupal. Filling code countless designs print_r (), dpr () and so on has long exhausted itself. Significantly the process is slowed down by "heavy" and confusing "dumps". Of course, if your code is filled to 100% unit-tests, the debugger is not necessary, but such projects are very small. Without call trace (tracing the call stack) sometimes is not possible to find the cause of errors or loss of data and instead of losing precious time, you can quickly track down the problem area code. It`s very difficult to optimize the code without profiling.

Part 1: Overview and Comparison

Technology of overview

The core of PHP acts Zend Engine (hereinafter ZE). The so-called layer manages memory, interpretation of the code, its compilation, execution, etc.; as ZE interacts and implementation of various third-party extensions. There are two types of extensions: Zend and (non-Zend). The difference between them, if briefly, in the level of implementation. Zend extensions are made literally in the nucleus, thereby providing performance and deep integration. Debugging situation just take as close as possible to get close to the code.

Pay attention to the two types of PHP: TS (thread safe) and NTS (non thread safe). For each species separate PHP extensions!

TS used on servers, where each request is given a separate thread (multithreading) and memory space. NTS is used on servers that do not support multithreading. For example the server IIS (Internet Information Service) from Microsoft, which, as you've probably guessed, runs on Windows, uses PHP NTS. Different servers use different types of PHP, optimized in different ways and used in different situations.

As can be seen from the diagram below: 

Regardless of the configuration backend (FPM, etc.)  debugging is possible if a special extension is installed. This is an extension for each query belonging to a debug session, binds to IDE (or other suitable program) through the port, which is listened by this IDE.

Debugging session is initiated on the user side (1 pt.). The session can last for one full page loading or until it will be canceled by the user. Session can be started as from the IDE, as from the browser. Regardless of the starting session place   initiation mechanism is as follows: cookies set  or URL parameters are added.

Once the backend receives the request and forwards it to PHP, extension of debugging receives data (2 p.) And establishes a connection (3 p.) Through the port that is listened by IDE (4 p.). Upon successful connection IDE captures the session and starts the debugging process in its context with the direct participation of the user.

There are a large number of IDE with a small number of extensions for debugging. We will study the most well-known of them. I'll start with extensions for debugging.


"XDebug", perhaps one of the most used and known extensions for debugging.

The author is one of the designers of PHP. Extension contains a lot of settings, support profiling. Also important fact that the extension has an open free license, is actively developed and has the most accessible and detailed documentation. Almost any version of PHP (including the TS and NTS species) can be installed XDebug. 

Most popular IDE such as: PhpStorm, NetBeans, PDT (based on Eclipse) - have built-in support for XDebug.

"Zend Debugger". Probably you know that the version of PHP 3.0 was written by the future founder of Zend Technologies. PHP, and is still being actively developed with the direct participation of the company, whose decisions affect the fate of PHP. 

Extension, created in the company, has the tight integration with ZE. This implies the performance, manufacturability and Optimization. But for all these charms have to pay a commercial license, which costs, alas, not small money, even by the standards of Western Europe and America.

Support for debugging with Zend Debugger can only boast PhpStorm and Zend Studio. Yes, and run an extension for new versions of PHP without Zend Server is not possible. But in serious situations "worth it", as will be discussed in the article about how to use Zend Debugger. 

"NuSphere PhpEd Debugger (abbr. Dbg)", this extension is associated with an IDE from NuSphere Company without a license for this extension PhpEd you cannot use. Paradoxically, in contrast to the "owners" PHP, developers of the company NuSphere able to create the most stable, productive and easy of all existing solutions, while with sufficient functionality. Installed on any version of PHP (5.2 and above, TS or NTS). Editor PhpEd today the fastest and most economical resource. The editor is able to open projects with 100 thousands of files, responsive work to show auto complete and hints for the code, with no visible delay while consuming a minimum of RAM. Debugging can be carried out even through SSH-Tunnel (tell more in a future article) without laborious configuration. Dbg, perhaps, is not looking for a license, the most successful extension for debugging.

I would be rather glad by PhpEd (and price with a "small scratches"), if there was support for Git "out of the box" in it  and  it had a wider functional of editing code (refactoring selection column, etc..). 


When comparing the license  the price was taken into account in the first place . In the documentation, except the presence of the installation instructions, is  welcomed detailed description of options and additional functionality. Performance measured on debugging modules panels and views in Drupal at cold start (first start) and at the transition points between stops (breakpoints). Functionality is determined by the available options. Quality of reporting - this is how much more debug information can be displayed. Finally, management is determined by the convenience of monitoring the debugging session: it's intuitive UI, and additional features such as progressive implementation and so on.

In the next article I will tell you nuances about debugging in PHP, Drupal и MySQL/MariaDB.