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:
- Laravel 9 Image Upload Tutorial With Example
- Laravel 9 Multiple Database Connections Example
- Laravel 9 Automatically Generates Sitemap With Example
- Make Model In Laravel 9 Tutorial With Example
- Laravel Firebase Tutorial With Example
- Multiple File Upload In Laravel 9 With Example
- Laravel 9 Multiple Images Upload Example
- Show All Fields Of Model In Django Admin
- Laravel 9 Multi Language Routes With Auth Routes
- How To Update Pivot Table In Laravel
- How To Install Vue In Laravel 8 Step By Step
- How To Handle Failed Jobs In Laravel
- Best Ways To Define Global Variable In Laravel
- How To Get Latest Records In Laravel
- Laravel Twilio Send SMS Tutorial With Example
- How To Pass Laravel URL Parameter
- Set Default Value Of Timestamp In Laravel Migration
- Laravel 9 File Upload Tutorial With Example
- How To Schedule Tasks In Laravel With Example
- 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!