Chapter 10: Error Handling and Debugging
Understand how to handle errors gracefully in PHP, and learn debugging techniques using tools like var_dump
and error logging.
In this chapter, we’ll explore error handling and debugging in PHP. Managing errors is essential for building robust applications, while effective debugging allows you to identify and fix issues quickly. We’ll cover common error handling methods, debugging techniques, and logging practices.
Types of Errors in PHP
PHP errors are categorized into several types:
- Notice: Minor issues, often non-critical, such as using an undefined variable.
- Warning: More serious than notices but non-fatal. Warnings do not stop script execution.
- Fatal Error: Severe errors that stop script execution, such as calling an undefined function.
Basic Error Handling with try
and catch
PHP allows you to handle errors using try
and catch
blocks, commonly used with exceptions:
<?php
try {
if (!file_exists("somefile.txt")) {
throw new Exception("File not found.");
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
?>
In this example, an exception is thrown if somefile.txt
does not exist, and the catch
block displays the error message.
Using var_dump
for Debugging
var_dump
is a useful tool for debugging, as it displays the type and value of variables:
<?php
$variable = [1, 2, 3];
var_dump($variable);
?>
In this example, var_dump
outputs detailed information about $variable
, including its data type and values.
Custom Error Handling with set_error_handler
PHP allows you to define a custom error handler function using set_error_handler
. Here’s an example:
<?php
function customError($errno, $errstr) {
echo "Error [$errno]: $errstr<br>";
}
set_error_handler("customError");
echo $undefinedVar; // Triggers custom error handler
?>
In this example, the customError
function handles errors and displays a custom error message.
Error Reporting Levels
You can control the level of errors displayed using error_reporting
. Common settings include:
<?php
error_reporting(E_ALL); // Report all errors
error_reporting(E_ERROR | E_WARNING); // Report errors and warnings only
?>
Setting error_reporting
allows you to specify which types of errors should be displayed, making it easier to manage error visibility.
Logging Errors to a File
To log errors for future review, you can configure PHP to send errors to a log file:
<?php
ini_set("log_errors", 1);
ini_set("error_log", "php-error.log");
trigger_error("This is a test error!", E_USER_WARNING);
?>
In this example, errors are logged to php-error.log
. Using logs is helpful for debugging issues in production environments without displaying errors to users.
Displaying Errors in Development
While developing, it’s useful to display errors directly on the screen. You can enable error display with:
<?php
ini_set("display_errors", 1);
ini_set("display_startup_errors", 1);
error_reporting(E_ALL);
?>
This configuration shows all errors during development, allowing you to quickly identify and fix issues. Be sure to disable this in production to avoid exposing sensitive information.
Summary and Next Steps
In this chapter, we covered error handling and debugging techniques in PHP, including try
and catch
blocks, var_dump
, custom error handlers, and error logging. Mastering these skills will help you build more reliable applications. In the next chapter, we’ll explore asynchronous programming techniques in PHP to handle tasks like running background processes and managing time-consuming operations.