Friday, 5 October 2018

How to get useful error messages in PHP?

For syntax errors, you need to enable error display in the php.ini. By default these are turned off because you don't want a "customer" seeing the error messages. Check this page in the PHP documentation for information on the 2 directives: error_reporting and display_errorsdisplay_errors is probably the one you want to change. If you can't modify the php.ini, you can also add the following lines to an .htaccess file:
php_flag  display_errors        on
php_value error_reporting       2039
You may want to consider using the value of E_ALL (as mentioned by Gumbo) for your version of PHP for error_reporting to get all of the errors. more info
3 other items: (1) You can check the error log file as it will have all of the errors (unless logging has been disabled). (2) Adding the following 2 lines will help you debug errors that are not syntax errors:
error_reporting(-1);
ini_set('display_errors', 'On');
(3) Another option is to use an editor that checks for errors when you type, such as PhpEd. PhpEd also comes with a debugger which can provide more detailed information. (The PhpEd debugger is very similar to xdebug and integrates directly into the editor so you use 1 program to do everything.)



You can include the following lines in the file you want to debug:
error_reporting(E_ALL);
ini_set('display_errors', '1');
This overrides the default settings in php.ini, which just make PHP report the errors to the log.



There is a really useful extension called "xdebug" that will make your reports much nicer as well.



If you are super cool, you might try:
$test_server = $_SERVER['SERVER_NAME'] == "127.0.0.1" || $_SERVER['SERVER_NAME'] == "localhost" || substr($_SERVER['SERVER_NAME'],0,3) == "192";

ini_set('display_errors',$test_server);
error_reporting(E_ALL|E_STRICT);
This will only display errors when you are running locally. It also gives you the test_server variable to use in other places where appropriate.
Any errors that happen before the script runs won't be caught, but for 99% of errors that I make, that's not an issue.



To persist this and make it confortale, you can edit your php.ini file. It is usually stored in /etc/php.ini or /etc/php/php.ini, but more local php.ini's may overwrite it, depending on your hosting provider's setup guidelines. Check a phpinfo() file for Loaded Configuration File at the top, to be sure which one gets loaded last.
Search for display_errors in that file. There should be only 3 instances, of which 2 are commented.
Change the uncommented line to:
display_errors = stdout



I recommend Nette Tracy for better visualization of errors and exceptions in PHP:



You can register your own error handler in PHP. Dumping all errors to a file might help you in these obscure cases, for example. Note that your function will get called, no matter what your current error_reporting is set to. Very basic example:
function dump_error_to_file($errno, $errstr) {
    file_put_contents('/tmp/php-errors', date('Y-m-d H:i:s - ') . $errstr, FILE_APPEND);
}
set_error_handler('dump_error_to_file');



The two key lines you need to get useful errors out of PHP are:
ini_set('display_errors',1);
 error_reporting(E_ALL);
As pointed out by other contributors, these are switched off by default for security reasons. As a useful tip - when you're setting up your site it's handy to do a switch for your different environments so that these errors are ON by default in your local and development environments. This can be achieved with the following code (ideally in your index.php or config file so this is active from the start):
switch($_SERVER['SERVER_NAME'])
{
    // local
    case 'yourdomain.dev':
    // dev
    case 'dev.yourdomain.com':
        ini_set('display_errors',1);
        error_reporting(E_ALL);
    break;
    //live
    case 'yourdomain.com':
        //...
    break;
}



You might also want to try PHPStorm as your code editor. It will find many PHP and other syntax errors right as you are typing in the editor.



ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);



To turn on full error reporting, add this to your script:
error_reporting(E_ALL);
This causes even minimal warnings to show up. And, just in case:
ini_set('display_errors', '1');
Will force the display of errors. This should be turned off in production servers, but not when you're developing.



The “ERRORS” are the most useful things for the developers to know their mistakes and resolved them to make the system working perfect.
PHP provides some of better ways to know the developers why and where their piece of code is getting the errors, so by knowing those errors developers can make their code better in many ways.
Best ways to write following two lines on the top of script to get all errors messages:
error_reporting(E_ALL);
ini_set("display_errors", 1);
Another way to use debugger tools like xdebug in your IDE.



http://todell.com/debug can be useful as well. You can see your object values or thrown debug errors behind the scene even in production mode.



0 comments:

Post a Comment