How can I get useful error messages in PHP?

How can I get useful error messages in PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How can I get useful error messages in PHP?

Running a PHP script often results in a blank screen instead of an informative error message. You might encounter a simple syntax error like a missing semicolon, a mismatched bracket, or a failed function call, yet the output remains completely empty. This forces you to revert to inefficient debugging methods: commenting out large blocks of code or scattering echo statements throughout your application just to locate where execution stops.

While this frustration is common among developers migrating from languages like Java, PHP lacks a default global exception handler that automatically surfaces stack traces in production environments without configuration. The solution isn't about guessing; it's about controlling how PHP handles fatal errors and uncaught exceptions.

The first step is ensuring your php.ini settings are aligned with your development needs. By default, many shared hosting environments or minimal installations have display_errors set to 0 or Off. This setting suppresses the output of error messages for security reasons, preventing sensitive data like file paths from being exposed in logs or browser responses.

To get useful error messages during development, you need to explicitly enable this behavior:

display_errors = On
error_reporting = E_ALL

Placing these directives in your active php.ini file or at the top of your application's entry point (like index.php) ensures that PHP stops execution on an error and prints a detailed message including the filename, line number, and a description of the issue. However, relying solely on display_errors = On is often insufficient for complex applications because it only catches fatal errors and parse errors by default in older PHP versions, or behaves inconsistently depending on the specific error type.

A more robust approach involves setting up an exception handler. You can create a custom class that extends \Throwable to intercept uncaught exceptions. This allows you to log the full stack trace while optionally redirecting the user to a friendly error page. Here is a basic implementation:

class CustomExceptionHandler {
    public static function register() {
        set_exception_handler(function ($exception) {
            // Log the error to a file or monitoring service here
            error_log($exception->getMessage() . "\n" . $exception->getTraceAsString());
            
            // Optionally redirect to a friendly page
            // header("Location: /error-page"); exit;
        });
    }
}

// Register the handler at your application bootstrap stage
CustomExceptionHandler::register();

This pattern ensures that even when display_errors is turned off for production, you still capture critical data. It also gives you control over what the end-user sees versus what gets recorded in your logs.

In a Laravel ecosystem, this concern is addressed out of the box through built-in exception handling and view composers. If you are building on top of Laravel, you can customize the App\Exceptions\Handler class to log errors or render specific views based on the environment. For instance, you might want detailed stack traces in local environments but a generic 500 page in production.

The trade-off here is between transparency and security. Displaying full stack traces in production can expose internal directory structures and database paths to attackers. Therefore, the best practice is to enable display_errors only during development or testing phases. In production, use logging mechanisms like Monolog or your server's native error logs to investigate issues without exposing them publicly.

By configuring these settings correctly, you eliminate the need for blind guessing and scattered debug prints, allowing you to focus on fixing the root cause of the failure rather than finding where it happened.

Our network