How do I get PHP errors to display?
This always works for me:
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
However, this doesn't make PHP to show parse errors - the only way to show those errors is to modify your php.ini with this line:
display_errors = on
(if you don't have access to php.ini
, then putting this line in .htaccess
might work too):
php_flag display_errors 1
How to display PHP errors in code output?
ini_set('display_errors', 1);
error_reporting(E_ALL ^ E_NOTICE);
Showing all errors and warnings
Display errors could be turned off in the php.ini
or your Apache configuration file.
You can turn it on in the script:
error_reporting(E_ALL);
ini_set('display_errors', '1');
You should see the same messages in the PHP error log.
PHP Error Reporting to Screen, with Line Breaks
You can use line breaks or HTML in your code to drop in what you need between warnings and errors:
$error = "some warning";
echo $error;
echo $error;
Will output:
some warningsome warning
However, you can add a BR to it to display properly in a browser:
$error = "some warning";
echo $error."<br />";
echo $error."<br />";
Which will output the following in a browser:
some warning
some warning
Alternately, you can use non html line breaks if you are using a terminal, or outputting text to the screen (rather than HTML):
$error = "some warning";
echo $error."\r\n";
echo $error."\r\n";
Which will put in line breaks into normal text.
You can mix and match these together to suit your needs. The other thing you can do is use <pre>
and </pre>
tags around your error messages so that they display into a browser and/or text file as the error message itself reads:
$error = "some warning";
echo "<pre>";
echo $error;
echo $error;
echo "</pre>";
Edit: If you are generating many warnings/errors that you aren't actually displaying on purpose, you can also modify the following:
html_errors = 1
in your PHP.ini file which will output things into a nice HTML format.
PHP command line run does not show errors
To override display_errors=Off
in php.ini, add a -d
flag to the command line:
php -d display_errors=on script.php
Or just edit the ini and turn the flag on.
How to avoid or restrict PHP Error output
To disable any error display to the user of your page, set
display_errors = Off
in your php.ini (this is the recommended setting for production websites anyway!) or
ini_set('display_errors', 0);
in your php.
This display_errors
setting only affects the output on the webpage; it will not affect a possibly configured logfile; there the messages still would be logged.
See the php documentation on configuring error reporting: http://php.net/manual/en/errorfunc.configuration.php
Note: The error_reporting
setting mentioned by other users here, will, to my knowledge, affect all kinds of error reports (i.e. also what is reported to a possibly configured log file). If you set error_reporting
to 0, you won't get any log entries as well. If you want to log something to the log file but not show it to the user, the display_errors
setting is the way to go.
php error reporting for single page?
Using error_reporting(E_ALL)
will enable error reporting for only that page!
To display errors use, ini_set("display_errors", 1)
How to see php error in included file while output buffer?
One option is to override error handlers, and call ob_end(); before printing out thrown error/warning/exception.
Other option, is to frequently check error log, if you encounter odd behaviour.
Get all PHP errors/warnings/... that occurred during the current request
Referring to your first concern
The error handling will still work as long as the:
callback function returns FALSE
function myErrorHandler($error_level, $error_message, $error_file, $error_line, $error_context) {
// Do your stuff here, e.g. saving messages to an array
// Tell PHP to also run its error handler
return false;
}
One solution to store all error-numbers (in an outside array $error_list
) could be:
$error_list = array();
$myErrorHandler = function ($error_level, $error_message, $error_file, $error_line, $error_context) use (&$error_list) {
$error_list[] = $error_level;
// Tell PHP to also run its error handler
return false;
};
// Set your own error handler
$old_error_handler = set_error_handler($myErrorHandler);
Another approach is to use Exceptions. There is a nice example in the comments for the function set_error_handler()
Also see:
- Are global variables in PHP considered bad practice? If so, why?
- PHP: Callback function using variables calculated outside of it
Referring to your second concern:
As Egg
already mentioned: using register_shutdown_function()
is a way to go, and the code in the answer https://stackoverflow.com/a/7313887/771077 shows you how.
But keep in mind, that
- Registering the shutdown- and error-handling functions should be the first thing in code (I fool once had an error in a config file, which was not handled correct, since I registered the error handler afterwards).
- Parse errors might not be handled correctly (See comment in https://stackoverflow.com/a/7313887/771077)
PHP not displaying errors even though display_errors = On
You also need to make sure you have your php.ini
file include the following set or errors will go only to the log that is set by default or specified in the virtual host's configuration.
display_errors = On
The php.ini
file is where base settings for all PHP on your server, however these can easily be overridden and altered any place in the PHP code and effect everything following that change. A good check is to add the display_errors
directive to your php.ini
file. If you don't see an error, but one is being logged, insert this at the top of the file causing the error:
ini_set('display_errors', 1);
error_reporting(E_ALL);
If this works then something earlier in your code is disabling error display.
Related Topics
PHP Switch Case More Than One Value in the Case
Get Key of Multidimensional Array
Google API How to Connect to Receive Values from Spreadsheet
Class 'App\Http\Controllers\Db' Not Found and I Also Cannot Use a New Model
How to Add Commas to Numbers in PHP
Safe Alternatives to PHP Globals (Good Coding Practices)
If You Create a Variable Inside a If Statement Is It Available Outside the If Statement
Which Is Fastest in PHP- MySQL or MySQLi
How to Send Money to Paypal Using PHP
How to Connect to Database on Another Server
Codeigniter: How to Do a Select (Distinct Fieldname) MySQL Query
How to Keep Form Values After Post