How to Show All Errors in PHP
-
Use the
error_reporting('E_ALL')
Function and theini_set()
Function Along With thedisplay_errors
Directive to Display Warning Error -
Use the
error_reporting('-1')
Function and theini_set()
Function Along With thedisplay_errors
anddisplay_startup_errors
Directives to Display Error -
Modify the Directives in the
php.ini
File to Display All Errors, Including Parse Errors in PHP
We will introduce a way to display errors in PHP script using error_reporting()
function and the ini_set()
function. The first method reports the error while the second method overrides the php.ini
file or the Apache configuration file and turns on to display the errors for the current script only.
We will demonstrate another way to display PHP errors using the same functions - the error_reporting()
function and the ini_set()
function as in the first method. The critical point that differentiates this method from the first one is that it uses another ini_set()
function with display_startup_errors
directive and error_reporting()
function takes an integer value as the argument.
We will introduce a permanent solution to display all the PHP errors, changing the configuration in the php.ini
file. The two methods mentioned above won’t help display the parse errors like missing curly braces and semicolons.
Use the error_reporting('E_ALL')
Function and the ini_set()
Function Along With the display_errors
Directive to Display Warning Error
We can use the error_reporting()
function to report all types of errors in the PHP script. For that, we use the named constant E_ALL
as the argument in the function. It reports all kinds of PHP errors. And then, we can use the ini_set()
function to display the reported errors. The function sets the directive display_errors
to true. Thus, it sets the configuration in the php.ini
file to set the errors’ visibility. However, the directives’ original value will be restored in the php.ini
file after executing the current script.
For demonstration, write the error_reporting(E_ALL)
as the first line of the script code. Set the display_errors
to 1
in the ini_set
function . Echo a message like 'This is a warning errorr'
. Use the include()
function to include an external file called file.php
. Then run the script.
The example below tries to display a warning error in PHP. This error type notifies that there is some problem in the script without stopping the script from running. The example includes the file file.php
in the script. Since there is no such file in the root folder, the script will not load the file and display the errors. As this is a warning error, the echo
statement runs along with the errors. To learn more about error_reporting()
function, please visit the PHP manual
Example Code:
# php 7.*
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
echo "This is a warning error";
include ("file.php");
?>
Output:
This is a warning error
**Warning**: include(file.php): failed to open stream: No such file or directory in **/var/www/html/index.php** on line **5**
**Warning**: include(): Failed opening 'file.php' for inclusion (include_path='.:/usr/share/php') in **/var/www/html/index.php** on line **5**
Use the error_reporting('-1')
Function and the ini_set()
Function Along With the display_errors
and display_startup_errors
Directives to Display Error
The error_reporting()
function also takes an integer value as the argument. We can use this method to display errors in PHP. There are a lot of levels of errors in PHP. The -1
level denotes all the PHP errors. Passing the value -1
will work in PHP’s future versions, even with new levels and constants. Sometimes, the display_errors
directive in the ini_set()
function does not always handle the errors encountered while the PHP starts. In such cases, we can use display_startup_errors
to display the errors.
For example, set the direcitves of the ini_set()
function as display_startup_errors
and display_errors
. Set them to be true
denoting by 1
in the ini_set()
function. Set error_reporting
to -1
. Assign a value of 10 to a variable $a
and print a variable $b
.
In the example below, the script encounters a notice error. The code tries to print the undefined variable. To learn more about ini_set()
function, please visit the PHP manual.
Example Code:
#php 7.x
<?php
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
$a = 10;
echo $b;
?>
Output:
**Notice**: Undefined variable: b in **/var/www/html/index.php** on line **6**
Modify the Directives in the php.ini
File to Display All Errors, Including Parse Errors in PHP
We can modify the directives in the php.ini
file to display PHP errors when the above-mentioned doesn’t work. The php.ini
file contains the default configurations needed while running a PHP script. The methods mentioned above change the directives’ value only in the runtime while modifying the php.ini
file permanently changes the values. However, we should not follow this practice at the production level. Check the PHP manual to know more about the configuration file in PHP.
To change the php.ini
file’s directives’ value, firstly locate the file in your system. Use the following script.
#php 7.x
<?php
phpinfo();
?>
Locate Loaded Configuration File
, and you can see the location of the php.ini
file. In the file, locate the directives display_errors
and display_startup_errors
and change the value to on
. Locate error_reporting
and change in into error_reporting = E_ALL ^ E_NOTICE ^ E_WARNING
. Then, restart Apache web server.
For example, in the PHP file, set the error_reporting(-1)
. Assign a value Orange
to the variable $a
but do not write a semicolon after it. Print the variable in another line.
In the output section of the code example, the parse error is encountered and displayed. Thus, configuring the php.ini
file can display the parse errors too.
Code Example:
#php 7.x
<?php
error_reporting(-1);
$a = "Orange"
echo $a;
?>
Output:
**Parse error**: syntax error, unexpected 'echo' (T_ECHO) in **/var/www/html/index.php** on line **6**
Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.
LinkedIn