PHP Error Handling
PHP error handling with try-catch and logging examples
Errors are a natural part of developing PHP applications. Proper error handling helps you:
Debug issues faster
Prevent exposing sensitive information
Provide user‑friendly messages
Log problems for developers
This tutorial explains all essential PHP error handling techniques with examples.

Common PHP error types include:
Parse Error – Syntax issues (missing semicolon, bracket)
Fatal Error – Script stops (undefined class/function)
Warning – Non‑fatal problems; script continues
Notice – Minor issues (undefined variable)
Exception – Errors thrown using throw and handled by try...catch
Use error_reporting() to show specific types of errors.
<?php
// Show all errors
error_reporting(E_ALL);
// Show warnings & errors only
error_reporting(E_ERROR | E_WARNING);
?>
ini_set('display_errors', 1);
ini_set('display_errors', 0);
ini_set('log_errors', 1);
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo $name; // Notice: undefined variable
?>
Exceptions allow graceful, controlled error handling.
<?php
function divide($a, $b) {
if ($b == 0) {
throw new Exception("Cannot divide by zero");
}
return $a / $b;
}
try {
echo divide(10, 0);
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
?>
<?php
try {
$pdo = new PDO("mysql:host=localhost;dbname=test", "root", "");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Database Error: " . $e->getMessage();
}
?>
set_error_handler()Create your own function to process PHP errors.
<?php
function myErrorHandler($errno, $errstr, $errfile, $errline) {
$msg = "Error [$errno]: $errstr in $errfile on line $errline";
error_log($msg);
echo "Something went wrong. Please try again later.";
return true; // Prevent default handler
}
set_error_handler("myErrorHandler");
echo $undefinedVar; // Triggers custom handler
?>
<?php
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});
try {
echo $undefinedVar;
} catch (ErrorException $e) {
echo "Caught exception: " . $e->getMessage();
}
?>
<?php
error_log("Custom log entry", 3, __DIR__ . "/app.log");
?>
Useful for debugging API failures, DB errors, etc.
<?php
function readFileSafe($file) {
if (!file_exists($file)) {
throw new Exception("File missing: $file");
}
$content = @file_get_contents($file);
if ($content === false) {
throw new Exception("Unable to read file: $file");
}
return $content;
}
try {
echo readFileSafe("data.txt");
} catch (Exception $e) {
error_log($e->getMessage());
echo "Unable to load file.";
}
?>
Show all errors in development; hide them in production
Log errors instead of showing them to users
Avoid exposing file paths, SQL queries, or stack traces
Use exceptions for DB queries, APIs, and risky operations
Always sanitize user input before using it
Convert PHP errors to exceptions for uniform handling
You learned:
Types of PHP errors
Error reporting and configuration
Try–catch blocks and exception handling
Custom error handlers
Safe file/database handling practices
Proper error handling improves security, performance, and user experience.