Debugging PHP Code: Techniques and Tools for Pros

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Debugging is a Developer's Superpower

\n\n

Have you ever sat at night, staring at your monitor, unable to understand why your PHP site is showing a white screen of death? Or, even worse, incorrect numbers in a report? Congratulations, you've encountered debugging. Many beginners are afraid of this word, thinking that debugging PHP code is only for the chosen gurus. In reality, it's just a skill that can and should be trained.

\n\n

In this article, we won't just list functions. We'll break down real scenarios: from simple var_dump() to configuring Xdebug in an IDE. You'll learn how to turn the process of finding errors into a structured routine, rather than guessing blindly. Let's go!

\n\n

1. The Classics: var_dump and print_r

\n\n

The simplest way to debug PHP is to insert var_dump($variable) in a suspicious place. But there's a nuance: not everyone does it correctly.

\n\n

Proper var_dump

\n

Many write it like this:

\n
var_dump($user);
\n

And get a messy output on the screen. It's better to format the output:

\n
echo '<pre>';\nvar_dump($user);\necho '</pre>';\ndie(); // stop execution so the rest of the output doesn't interfere
\n\n

And for arrays and objects, print_r() with the true flag is more convenient to save the result as a string:

\n
$data = print_r($users, true);\nfile_put_contents('debug.log', $data, FILE_APPEND);
\n\n

Tip: Never leave var_dump in production. Use conditional debugging:

\n
if (defined('DEBUG_MODE') && DEBUG_MODE) {\n    var_dump($variable);\n}
\n\n

2. Logging: Your Best Friend in Production

\n\n

When the site is already on the server, you can't just output an error to the screen. That's where PHP logging comes to the rescue. The built-in error_log() function is the foundation.

\n\n

How to Configure error_log

\n

In the php.ini file, specify:

\n
error_reporting = E_ALL\ndisplay_errors = Off\nlog_errors = On\nerror_log = /var/log/php_errors.log
\n\n

Now all errors, including warnings, will be written to a file. And in the code, you can do this:

\n
if ($result === false) {\n    error_log("Database query error: " . $db->error, 3, '/var/log/myapp.log');\n    // 3 — means write to a file, not to syslog\n}
\n\n

Modern Approach: Monolog

\n

For serious projects, use the Monolog library. It can write to files, Slack, databases, and even Telegram. Example setup:

\n
use Monolog\\Logger;\nuse Monolog\\Handler\\StreamHandler;\n\n$log = new Logger('my_app');\n$log->pushHandler(new StreamHandler(__DIR__ . '/app.log', Logger::WARNING));\n$log->warning('User attempted to access without permissions', ['user_id' => 123]);
\n\n

3. Xdebug: The Reactive Debugging Engine

\n\n

If you haven't installed Xdebug yet, you're wasting time. This isn't just a tool — it's a jetpack for developers. It allows you to set breakpoints, view variable values in real-time, and even profile code.

\n\n

Installation and Configuration

\n

Install via package manager:

\n
pecl install xdebug\n# or for Ubuntu:\nsudo apt install php-xdebug
\n\n

Add to php.ini:

\n
zend_extension=xdebug.so\nxdebug.mode=debug\nxdebug.start_with_request=yes\nxdebug.client_host=127.0.0.1\nxdebug.client_port=9003
\n\n

Now configure your IDE. In PhpStorm, this is done in two clicks: configure the PHP interpreter and click "Start Listening for PHP Debug Connections".

\n\n

How It Works in Practice

\n

Place a breakpoint (red circle) on the line with suspicious code. Run the script in the browser with the parameter ?XDEBUG_SESSION_START=1.

Blogs

Book Recommendations