PHP Displaying errors


Example

If you want PHP to display runtime errors on the page, you have to enable display_errors, either in the php.ini or using the ini_set function.

You can choose which errors to display, with the error_reporting (or in the ini) function, which accepts E_* constants, combined using bitwise operators.

PHP can display errors in text or HTML format, depending on the html_errors setting.

Example:

ini_set("display_errors", true);
ini_set("html_errors", false); // Display errors in plain text
error_reporting(E_ALL & ~E_USER_NOTICE); // Display everything except E_USER_NOTICE

trigger_error("Pointless error"); // E_USER_NOTICE
echo $nonexistentVariable; // E_NOTICE
nonexistentFunction(); // E_ERROR

Plain text output: (HTML format differs between implementations)

Notice: Undefined variable: nonexistentVariable in /path/to/file.php on line 7

Fatal error: Uncaught Error: Call to undefined function nonexistentFunction() in /path/to/file.php:8
Stack trace:
#0 {main}
  thrown in /path/to/file.php on line 8

NOTE: If you have error reporting disabled in php.ini and enable it during runtime, some errors (such as parse errors) won't be displayed, because they occured before the runtime setting was applied.

The common way to handle error_reporting is to enable it fully with E_ALL constant during the development, and to disable publicly displaying it with display_errors on production stage to hide the internals of your scripts.