Home » How To Display All Errors In PHP

How To Display All Errors In PHP

Last updated on June 10, 2022 by

PHP has many levels of errors, we can use in-built functions to handle all these levels. So in this tutorial, we will show you how to display all errors in PHP with different levels. Let’s just jump into it.

1. Best And Simple Way

The easiest way to display all errors, notices, and warnings in PHP is to add the following lines to your PHP code file. Add the following code into your main PHP file like header.php or function.php before the start of any PHP code.

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

A good and ideal way to display all errors in PHP should be based on the environment variable. If you are working on a live site then you don’t want to expose the errors, warnings, or notices on the live site. Then you simply add the below code and define your environment.

<?php

if(!defined('ENVIRONMENT')){
    define('ENVIRONMENT', 'DEVELOPMENT');
}

if (defined('ENVIRONMENT')) {

    switch (ENVIRONMENT) {
        case 'DEVELOPMENT':
            ini_set('display_errors', 1);
            ini_set('display_startup_errors', 1);
            error_reporting(E_ALL|E_STRICT);
            break;

        case 'PRODUCTION':
            error_reporting(0);
            break;

        default:
            exit('The application environment is not set correctly.');
    }
}

2. Display All Errors Via php.ini File

You can permanently handle the behavior of errors using the php.ini file. Go to the php.ini file and turn on the below setting:

display_errors = on

The display_errors setting will show you all types of errors including syntax errors, parse errors, warnings, notices, etc.

Notes: After changing the php.ini file, you need to restart the apache server. If you are on an ubuntu server then you need to run the command sudo service apache2 restart. The apache2 restart command vary based on your OS.

3. Display All Errors Via .htaccess File

Sometimes, it happens that you can’t able to find php.ini file on the hosting server at that time you can use the .htaccess file to display all PHP errors.

Most of the web apps have the .htaccess file in the root directory or possible it might be in another directory or might have no .htaccess file. You can create a .htaccess file in the root directory of your project, for example, public_html, or open the existing one and add the below code into it to display all PHP errors.

php_flag display_startup_errors on
php_flag display_errors on

Additionally, read our guide:

  1. Laravel 9 Image Upload Tutorial With Example
  2. Laravel 9 Multiple Database Connections Example
  3. Laravel 9 Automatically Generates Sitemap With Example
  4. Make Model In Laravel 9 Tutorial With Example
  5. Laravel Firebase Tutorial With Example
  6. Multiple File Upload In Laravel 9 With Example
  7. Laravel 9 Multiple Images Upload Example
  8. Show All Fields Of Model In Django Admin
  9. Laravel 9 Multi Language Routes With Auth Routes
  10. How To Update Pivot Table In Laravel
  11. How To Install Vue In Laravel 8 Step By Step
  12. How To Handle Failed Jobs In Laravel
  13. Best Ways To Define Global Variable In Laravel
  14. How To Get Latest Records In Laravel
  15. Laravel Twilio Send SMS Tutorial With Example
  16. How To Pass Laravel URL Parameter
  17. Set Default Value Of Timestamp In Laravel Migration
  18. Laravel 9 File Upload Tutorial With Example
  19. How To Schedule Tasks In Laravel With Example
  20. Laravel Collection Push() And Put() With Example

That’s it from our end. We hope this article helped you to learn how to display all errors in PHP.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post 🙂 Keep Smiling! Happy Coding!

 
 

Leave a Comment