Introduction: Why Debugging is a Developer's Superpower
\n\nHave 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\nIn 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!
1. The Classics: var_dump and print_r
\n\nThe 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.
Proper var_dump
\nMany write it like this:
\nvar_dump($user);\nAnd get a messy output on the screen. It's better to format the output:
\necho '<pre>';\nvar_dump($user);\necho '</pre>';\ndie(); // stop execution so the rest of the output doesn't interfere\n\nAnd for arrays and objects, print_r() with the true flag is more convenient to save the result as a string:
$data = print_r($users, true);\nfile_put_contents('debug.log', $data, FILE_APPEND);\n\nTip: Never leave var_dump in production. Use conditional debugging:
if (defined('DEBUG_MODE') && DEBUG_MODE) {\n var_dump($variable);\n}\n\n2. Logging: Your Best Friend in Production
\n\nWhen 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.
How to Configure error_log
\nIn the php.ini file, specify:
error_reporting = E_ALL\ndisplay_errors = Off\nlog_errors = On\nerror_log = /var/log/php_errors.log\n\nNow all errors, including warnings, will be written to a file. And in the code, you can do this:
\nif ($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\nModern Approach: Monolog
\nFor serious projects, use the Monolog library. It can write to files, Slack, databases, and even Telegram. Example setup:
\nuse 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\n3. Xdebug: The Reactive Debugging Engine
\n\nIf 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\nInstallation and Configuration
\nInstall via package manager:
\npecl install xdebug\n# or for Ubuntu:\nsudo apt install php-xdebug\n\nAdd to php.ini:
zend_extension=xdebug.so\nxdebug.mode=debug\nxdebug.start_with_request=yes\nxdebug.client_host=127.0.0.1\nxdebug.client_port=9003\n\nNow configure your IDE. In PhpStorm, this is done in two clicks: configure the PHP interpreter and click "Start Listening for PHP Debug Connections".
\n\nHow It Works in Practice
\nPlace a breakpoint (red circle) on the line with suspicious code. Run the script in the browser with the parameter ?XDEBUG_SESSION_START=1.