How to display errors?

Displaying errors in PHP is essential for diagnosing issues during development and ensuring the smooth operation of your web applications. PHP provides several ways to display errors, ranging from simple on-screen messages to more advanced logging mechanisms. Here’s how to display errors effectively in PHP:


  1. Error Reporting Level:

   – Adjust the error reporting level using the `error_reporting` directive in your PHP configuration or the `error_reporting()` function within your PHP script. Setting it to `E_ALL` ensures that all errors, notices, and warnings are reported.


  1. Display Errors On-Screen:

   – During development, displaying errors directly on the screen can be helpful for debugging. Enable error display by setting the `display_errors` directive to `On` in your PHP configuration.

display_errors = On


   Alternatively, you can enable error reporting in your PHP script using `ini_set()`:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

  1. Custom Error Pages:

   – For production environments, it’s not advisable to display error messages to end-users, as they can reveal sensitive information. Instead, create custom error pages that provide a user-friendly message while logging the detailed error on the server.


  1. Logging Errors:

   – Use PHP’s error logging functions, such as `error_log()`, to record errors in log files. This is crucial for debugging and monitoring applications in production. You can specify the log file location and format as needed.

error_log("An error occurred: This is a custom error message", 3, "/var/log/php_errors.log");
  1. Handling Exceptions:

   – For more severe errors and exceptions, use `try-catch` blocks to handle and report exceptions gracefully. This allows you to provide specific error-handling logic.


By following these practices, you can effectively manage and display errors in PHP. During development, showing errors on-screen aids in debugging, while in production, it’s essential to log errors for monitoring and security, displaying user-friendly messages, and creating custom error pages to maintain a positive user experience.

Previously at
Flag Argentina
time icon
Full Stack Engineer with extensive experience in PHP development. Over 11 years of experience working with PHP, creating innovative solutions for various web applications and platforms.