Debugging is one of the most important skills in PHP development. Whether youβre dealing with syntax errors, logical bugs, or performance issues, knowing how to efficiently debug your PHP application can save you hours of frustration.
π― In this guide, youβll learn:
β
Common PHP errors and how to fix them
β
Using built-in debugging functions (var_dump
, print_r
)
β
Logging errors for debugging in production
β
How to use Xdebug for advanced debugging
β
Best tools for tracking performance issues
By the end, youβll know how to debug PHP applications like a pro and make your development process faster and smoother. π
1οΈβ£ Common PHP Errors and How to Fix Them
Before diving into debugging tools, letβs first understand the types of errors youβll commonly encounter in PHP.
Error Type | Description | Example |
---|---|---|
Parse Error | Syntax issue in PHP code | Parse error: Unexpected 'echo' |
Fatal Error | Missing function or class | Call to undefined function myFunc() |
Warning | Non-fatal issue | Warning: include(file.php): failed to open |
Notice | Minor error, but can cause bugs | Notice: Undefined variable $name |
2οΈβ£ Enabling Error Reporting in PHP
To see all errors, enable full error reporting in development mode.
Add this to your PHP script:
error_reporting(E_ALL);
ini_set("display_errors", 1);
π₯ Why use this?
β
Shows all errors and warnings
β
Helps debug issues faster
3οΈβ£ Debugging with var_dump()
and print_r()
The simplest way to debug PHP is by inspecting variables using var_dump()
or print_r()
.
Example 1: Using var_dump()
<?php
$person = ["name" => "Zero Dev", "age" => 30];
var_dump($person);
?>
π‘ Output:
array(2) {
["name"]=> string(8) "Zero Dev"
["age"]=> int(30)
}
π₯ Best for:
β
Viewing data types and structures
Example 2: Using print_r()
<?php
print_r($person);
?>
π‘ Output:
Array ( [name] => Zero Dev [age] => 30 )
π₯ Best for:
β
Simple readable output
4οΈβ£ Logging Errors for Debugging in Production
In production, you should not display errors to users but instead log them.
Enable Error Logging
Edit php.ini
:
log_errors = On
error_log = /var/log/php_errors.log
Or dynamically set in PHP:
ini_set("log_errors", 1);
ini_set("error_log", "php_errors.log");
Example: Logging Errors
<?php
error_log("This is an error message!", 3, "php_errors.log");
?>
π₯ Why log errors?
β
Prevents users from seeing sensitive debug info
β
Helps developers find issues in live environments
5οΈβ£ Using Xdebug for Advanced Debugging
π‘ Xdebug is a powerful PHP extension that provides better error messages, stack traces, and step debugging.
1οΈβ£ Install Xdebug
For Ubuntu:
sudo apt install php-xdebug -y
For Mac (Homebrew):
brew install php-xdebug
2οΈβ£ Enable Xdebug
Edit php.ini
:
zend_extension=xdebug.so
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.log_level=0
Restart PHP:
sudo systemctl restart php8.2-fpm
3οΈβ£ Use Xdebug in VS Code
1οΈβ£ Install the PHP Debug extension in VS Code.
2οΈβ£ Configure launch.json
:
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003
}
]
}
3οΈβ£ Set breakpoints and run the debugger! π
π₯ Why use Xdebug?
β
Step through code line by line
β
See function call stack
β
Inspect variables in real time
6οΈβ£ Debugging Performance Issues with Profiling
π‘ Performance debugging helps find slow database queries, memory leaks, and bottlenecks.
1οΈβ£ Use microtime()
to Measure Execution Time
<?php
$start = microtime(true);
sleep(1);
$end = microtime(true);
echo "Execution Time: " . ($end - $start) . " seconds";
?>
π‘ Output:
Execution Time: 1.0004 seconds
π₯ Best for:
β
Finding slow functions and loops
2οΈβ£ Use Xdebug Profiler
Enable profiling in php.ini
:
xdebug.mode=profile
xdebug.output_dir="/tmp"
Run your script and check /tmp
for the .cachegrind
file.
π₯ Analyze results using Webgrind.
7οΈβ£ Debugging Database Queries
π‘ Slow database queries are a common PHP performance issue.
1οΈβ£ Enable MySQL Query Logging
Run in MySQL:
SET GLOBAL general_log = 'ON';
SET GLOBAL log_output = 'TABLE';
Check slow queries:
SELECT * FROM mysql.general_log ORDER BY event_time DESC LIMIT 10;
π₯ Why?
β
Helps identify slow queries
β
Reduces unnecessary database calls
2οΈβ£ Debug Queries in PHP
<?php
$conn = new PDO("mysql:host=localhost;dbname=mydb", "root", "");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([1]);
// Debug query execution time
echo "Query executed in: " . $stmt->queryString;
?>
π₯ Why?
β
Detects poorly optimized queries
8οΈβ£ Best Debugging Tools for PHP
Tool | Features |
---|---|
Xdebug | Step debugging, stack traces, profiling |
Whoops | Pretty error pages for debugging |
Telescope (Laravel) | Debugging Laravel applications |
Blackfire | PHP performance monitoring |
Webgrind | Visual Xdebug profiling analysis |
π Final Thoughts
Now you know how to debug PHP applications like a pro!
β
Use var_dump()
and print_r()
for quick debugging
β
Log errors instead of displaying them in production
β
Use Xdebug for step debugging
β
Profile performance bottlenecks with microtime() and Xdebug
β
Optimize slow database queries for better speed
π Next: Working with WebSockets in PHP for Real-Time Apps
Happy debugging! ππ