Categories
debugging error-handling php

How can I get useful error messages in PHP?

634

Quite often I will try and run a PHP script and just get a blank screen back. No error message; just an empty screen. The cause might have been a simple syntax error (wrong bracket, missing semicolon), or a failed function call, or something else entirely.

It is very difficult to figure out what went wrong. I end up commenting out code, entering “echo” statements everywhere, etc. trying to narrow down the problem. But there surely must be a better way, right?

Is there a way to get PHP to produce a useful error message, like Java does?

4

522

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_errors. display_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.)

Cartman’s link is also very good: http://www.ibm.com/developerworks/library/os-debug/

3

  • 30

    2039 is the value of E_ERROR | E_WARNING | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING | E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE. See docs.php.net/manual/en/errorfunc.constants.php

    – Gumbo

    May 10, 2009 at 17:59

  • I like the option of .htaccess file. It helps me debug in an area that is not part of the public website. Thanks a lot for this tip!

    – jacekn

    Jun 15, 2013 at 1:39

  • 1

    I would add that logging errors to file (and looking them up there) is the best solution. Don’t rely on displaying errors on-page – they can ruin it, you can forget to turn error reporting for production site and this will cause you trouble in future

    Mar 26, 2016 at 20:44

465

The following enables all errors:

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

Also see the following links

6

  • 29

    Best to make these changes at the .ini file level. Turning on error reporting from within a script is useless, as it won’t help with syntax errors or other fatal errors that kill the compile phase. The script gets killed long before it begins executing and reaches the reporting overrides.

    – Marc B

    Jul 4, 2011 at 19:49

  • You are correct indeed. I did not notice that the move is to your own server.

    – Eljakim

    Jul 4, 2011 at 19:51

  • 6

    Run phpinfo() to find the correct php.ini file. Look for the Loaded Configuration File line.

    – borrible

    Jul 5, 2011 at 8:01

  • 1

    If you are looking for errors that occur during the compile phase, check your apache logs often located at /var/log/apache2/error.log

    – csi

    Feb 21, 2014 at 22:08

  • 2

    This answer will fail on php7 when strict typing is enabled, because the second parameter of ini_set is a string.

    – PeeHaa

    Sep 4, 2015 at 18:16

179

The following code should display all errors:

<?php

// ----------------------------------------------------------------------------------------------------
// - Display Errors
// ----------------------------------------------------------------------------------------------------
ini_set('display_errors', 'On');
ini_set('html_errors', 0);

// ----------------------------------------------------------------------------------------------------
// - Error Reporting
// ----------------------------------------------------------------------------------------------------
error_reporting(-1);

// ----------------------------------------------------------------------------------------------------
// - Shutdown Handler
// ----------------------------------------------------------------------------------------------------
function ShutdownHandler()
{
    if(@is_array($error = @error_get_last()))
    {
        return(@call_user_func_array('ErrorHandler', $error));
    };

    return(TRUE);
};

register_shutdown_function('ShutdownHandler');

// ----------------------------------------------------------------------------------------------------
// - Error Handler
// ----------------------------------------------------------------------------------------------------
function ErrorHandler($type, $message, $file, $line)
{
    $_ERRORS = Array(
        0x0001 => 'E_ERROR',
        0x0002 => 'E_WARNING',
        0x0004 => 'E_PARSE',
        0x0008 => 'E_NOTICE',
        0x0010 => 'E_CORE_ERROR',
        0x0020 => 'E_CORE_WARNING',
        0x0040 => 'E_COMPILE_ERROR',
        0x0080 => 'E_COMPILE_WARNING',
        0x0100 => 'E_USER_ERROR',
        0x0200 => 'E_USER_WARNING',
        0x0400 => 'E_USER_NOTICE',
        0x0800 => 'E_STRICT',
        0x1000 => 'E_RECOVERABLE_ERROR',
        0x2000 => 'E_DEPRECATED',
        0x4000 => 'E_USER_DEPRECATED'
    );

    if([email protected]_string($name = @array_search($type, @array_flip($_ERRORS))))
    {
        $name="E_UNKNOWN";
    };

    return(print(@sprintf("%s Error in file \xBB%s\xAB at line %d: %s\n", $name, @basename($file), $line, $message)));
};

$old_error_handler = set_error_handler("ErrorHandler");

// other php code

?>

The only way to generate a blank page with this code is when you have a error in the shutdown handler. I copied and pasted this from my own cms without testing it, but I am sure it works.

9

  • 4

    I get a blank page from that code. What do you mean by “you have a error in the shutdown handler” and what should I do to solve the problem?

    – Paolo M

    Sep 24, 2014 at 23:08

  • @PaoloM, He is saying an error in the function ShutdownHandler above. Basically this is a stopgap hack in place of proper error handling.

    – Pacerier

    Oct 14, 2014 at 9:33

  • Thanks, was useful, but how can I disable E_NOTICE errors in this function?

    – MajAfy

    Apr 4, 2015 at 8:45

  • This is the right solution, but be careful with information discosure when error occurs… (prefer logging instead of echoing to users)

    Aug 29, 2016 at 14:13

  • 1

    I am using this when the Symfony can’t catch correctly fatal errors.

    – COil

    Sep 22, 2016 at 10:19