In this article, I am going to explain you about how to show errors in php.
By using below code we will display errors in php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
In php.ini by set display_errors as on by using below code.
display_errors = on
Common PHP Errors Types in PHP
- E_ALL: Catches all errors and warnings
- - E_CORE_ERROR: Fatal errors that occur during PHP's initial startup (installation)
- E_ERROR: A fatal error that causes script termination
- E_USER_NOTICE: User-generated notice message.
- E_PARSE: Compile time parse error.
- E_NOTICE: Run time notice caused due to error in code
- E_USER_WARNING: User-generated warning message.
- E_WARNING: Run-time warning that does not cause script termination
- E_CORE_WARNING: Warnings that occur during PHP's initial startup
- E_COMPILE_ERROR: Fatal compile-time errors indication problem with script.
- E_USER_ERROR: User-generated error message.
- E_STRICT: Run-time notices.
- E_RECOVERABLE_ERROR: Catchable fatal error indicating a dangerous error
Types of Errors in PHP
There are four basic types of runtime errors in PHP:
1. Fatal errors:
These are critical errors - for example, instantiating an object of a non-existent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHP's default behavior is to display them to the user when they take place.
2. Notices:
These are small, non-critical errors that PHP encounters while executing a script - for example, accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all - although the default behavior can be changed.
3. Warnings:
Warnings are more severe errors like attempting to include() a file which does not exist. By default, these errors are displayed to the user, but they do not result in script termination.
4.Parse Error :
it’s syntax error or missing code error.
it will stop script execution.
Examples for Warnings and Fatal error:
Warnings in PHP are represented by E_WARNING. When these error occur execution of the script is not halted. For example,
by using include function added one php file but forgot to create this file in this root directoy we will get warnings.
Fatal error are represented by E_ERROR. By using require function added one php file but forgot include that file we got fatal error.
Simple example for warnings:
<h1>QA With Experts</h1>
<h2>How to show errors in php</h2>
<p></p>
include("dept.php");
But i forgot include dept.php
i got below warning i got output.
see the below image for this example.
Simple for Fatal errors
my sample php code as shown bleow
<h1>QA With Experts</h1>
<h2>How to show errors in php</h2>
<p></p>
<?php require("dept.php");?>
But i forgot include dept.php in my code.
i got fatal error like this.
if you got fatal error our code terminated.So take care of this type of errors.
Here the functions include and require include one file into another file.
Difference between require, require_once, include and include_once
All these functions require, require_once, include and include_once are used to include the files in the php page but there is the slight difference between these functions.
Difference between require, require_once, include, include_once
Difference between require and include is that if the file you want to include is not found then include function give you a warning and executes the remaining code in of php page where you write the include function. While require gives you fatal error if the file you want to include is not found and the remaining code of the php page will not execute.
Example for include function:
include("dept.php");
By using this function we can include dept.php file in our php file.
Example for inclide_once function:
include_once("dept.php");
here include and include_once functions are same.but dept.php file included in our file multiple time it will include only one time.
Example for require function:
require("dept.php");
it is like include function.But the only difference is it returns the fatal error if included file missed as include function returns warnings.
Example for require_once function:
require_once("dept.php");
it is same as include_once funcitons.
Out of all require_once is best.
Then based on your requirement you can choose which you want to show :
For All Error, Warning and Notice
error_reporting(E_ALL); OR error_reporting(-1);
For All Errors
error_reporting(E_ERROR);
For All Warnings
error_reporting(E_WARNING);
For All Notice
error_reporting(E_NOTICE);
By Using htaccess we will display errors in php
see the bleow code in .htaccess
.htaccess:
php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on
php_flag log_errors on
php_value error_log /localhost/php_examples/PHP_errors.log
PHP error_log() Function
By using this function Send error messages to the web server's error log
Sample code for this
// Failed to connect to database tracking if error connecting to the database
if (!mysqli_connect("localhost","root","password","qa_db")) {
error_log("Failed to connect to MY DataBase!", 0);
}
if we got DB connection error in our error_log.txt file that error tacked like "Failed to connect ot MY DataBase!" as shown in the above example
By using following actions we will get errors info
1) Setting PHP error reporting to on in php.ini file
2) Setting PHP error reporting to on in .htaccess file
3) Getting information from error log file.