Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 8 of 9
Error Reporting & Debugging Setup
PHP has a layered error system - errors, warnings, notices, and exceptions. Proper error configuration is the difference between a production app that fails silently and logs correctly versus a development setup that shows you every issue immediately. Never display errors in production.
PHP Error System & Configuration
<?php
declare(strict_types=1);
// ── Error levels ──────────────────────────────────────────────────
// E_ERROR - fatal, execution stops
// E_WARNING - non-fatal, execution continues
// E_NOTICE - minor issues (using undefined variable)
// E_ALL - all errors (use in development)
// E_STRICT - interoperability suggestions
// ── Development configuration (php.ini or at runtime) ─────────────
ini_set("display_errors", "1");
ini_set("display_startup_errors", "1");
error_reporting(E_ALL);
// ── Production configuration - NEVER show errors to users ──────────
// In php.ini (production):
// display_errors = Off
// log_errors = On
// error_log = /var/log/php/error.log
// ── Controlling at runtime (useful for different environments) ──────
$env = getenv("APP_ENV") ?: "production";
if ($env === "development") {
ini_set("display_errors", "1");
error_reporting(E_ALL);
} else {
ini_set("display_errors", "0");
error_reporting(0);
ini_set("log_errors", "1");
ini_set("error_log", __DIR__ . "/../storage/logs/php_error.log");
}
// ── trigger_error - generate custom errors ────────────────────────
function divide(int $a, int $b): float {
if ($b === 0) {
trigger_error("Division by zero in divide()", E_USER_WARNING);
return 0.0;
}
return $a / $b;
}
// ── try/catch - exception handling ───────────────────────────────
try {
$pdo = new PDO("mysql:host=localhost;dbname=blog", "root", "wrong_pass");
} catch (PDOException $e) {
// Log the real error - never expose DB credentials/structure to users
error_log("DB connection failed: " . $e->getMessage());
// Show a safe message to users:
die("Service temporarily unavailable. Please try again later.");
}
// ── Error suppression @ - AVOID ───────────────────────────────────
// $file = @file_get_contents("missing.txt"); // Silently fails - bad practice
// ✅ Instead:
if (!file_exists("data.txt")) {
throw new RuntimeException("data.txt not found");
}
$file = file_get_contents("data.txt");Quick Quiz
Tip
Tip
Practice Error Reporting Debugging Setup in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for Error Reporting & Debugging Setup
Practice Task
Note
Practice Task - (1) Write a working example of Error Reporting Debugging Setup from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Error Reporting Debugging Setup is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready php code.
Finished this lesson?