We can show all errors in PHP using the error_reporting() function. It sets the error_reporting directive at runtime according to the level provided. If no level is provided, it will return the current error reporting level. error_reporting(E_ALL) level represents all errors, warnings, notices, etc.
PHP Code: If the level is set to zero, no error is reported even when an unavailable file is included.
PHP
<?php // Setting error reporting to zero error_reporting (0); // Including a file that is not present include (gfg.php); ?> |
Output:
No output No error is reported
PHP Code: When the level is set to E_ALL, all the errors including all warnings and notices are reported.
PHP
<?php // Setting error reporting to all errors error_reporting (E_ALL); // Including a file that is not present include (gfg.php); ?> |
Output:
PHP Notice: Use of undefined constant gfg - assumed 'gfg' in /home/36649711d0ef1764ba295676144de779.php on line 5 PHP Notice: Use of undefined constant php - assumed 'php' in /home/36649711d0ef1764ba295676144de779.php on line 5 PHP Warning: include(gfgphp): failed to open stream: No such file or directory in /home/36649711d0ef1764ba295676144de779.php on line 5
PHP Code: When the level is set to E_NOTICE, all the errors including undeclared variables are reported.
PHP
<?php // Setting error reporting to show undeclared variables error_reporting (E_NOTICE); // Including a file that is not present include (gfg.php); ?> |
Output:
Notice: Use of undefined constant gfg - assumed 'gfg' in C:\xampp\htdocs\showallErrorsinPHP\index3.php on line 6 Notice: Use of undefined constant php - assumed 'php' in C:\xampp\htdocs\showallErrorsinPHP\index3.php on line 6
PHP Code: When the level is set to ~E_NOTICE, all the errors do not show the notices.
PHP
<?php // Setting error reporting not to show notices error_reporting (~E_NOTICE); // Including a file that is not present include (gfg.php); ?> |
Output:
Warning: include(gfgphp): failed to open stream: No such file or directory in C:\xampp\htdocs\showallErrorsinPHP\index4.php on line 6 Warning: include(): Failed opening 'gfgphp' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\showallErrorsinPHP\index4.php on line 6
Other PHP errors: The fastest way to display all php errors and warnings is to add the following lines to your PHP code file.
ini_set('display_errors', 1); ini_set('display_startup_errors', 1);
Note: The display_errors and display_startup_errors are just two of the directives that are available. The display_errors directive will determine if the errors will be displayed or hidden from the user. The display_errors directive must be set to “on” in the PHP.ini file. The directive for showing PHP errors can also be enabled or disabled using the .htaccess file located in the root or public directory of the project.
References: