search

Home  >  Q&A  >  body text

How to get useful error messages in PHP?

<p>I often try to run a PHP script and just get a blank screen. No error message; just an empty screen. The cause could be a simple syntax error (wrong parentheses, missing semicolon), or a failed function call, or something else entirely. </p> <p>It's hard to pinpoint the problem. I ended up commenting out the code, typing "echo" statements here and there, etc. to try and narrow down the problem. But there has to be a better way, right? </p> <p>Is there a way to make PHP produce useful error messages, like Java does? </p>
P粉283559033P粉283559033488 days ago562

reply all(2)I'll reply

  • P粉190883225

    P粉1908832252023-08-24 14:59:54

    The following enables all errors:

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

    Also see the following links

    reply
    0
  • P粉575055974

    P粉5750559742023-08-24 14:00:05

    By default, Show Errors is turned off because you don't want "customers" to see error messages.

    Check out this page in the PHP documentation for information on 2 directives: error_reporting and display_errors. display_errors may be errors you want to change.

    So you have 3 options:

    (1) You can check the error log file as it will contain all errors (unless logging has been disabled). To enable error logging, make sure the log_errors configuration directive is set to On. Logs are also useful when the error does not occur in PHP but is issued by the web server.

    (2) You can add the following two lines, which will help you debug non-syntax errors that occur in the same file:

    error_reporting(E_ALL);
    ini_set('display_errors', 'On');

    Note that on a live server, the latter should be set to off (but only the latter, as you still need to know from the log file any errors that occur).

    However, for syntax errors occurring in the same file, the above commands will not work and you need to enable them in php.ini. If you can't modify php.ini, you can also try adding the following lines to your .htaccess file, although this is rarely supported these days:

    php_flag  display_errors        on
    php_value error_reporting       -1

    (3) Another option is to use an editor that checks for errors as you type, such as PhpEd, VSCode, or PHPStorm. They all come with a debugger that provides more detailed information. (The PhpEd debugger is very similar to xdebug and is integrated directly into the editor, so you can do it all with 1 program.)

    reply
    0
  • Cancelreply