2009-05-10 09:48 阅读 430

如何在 PHP 中获取有用的错误消息?

I find programming in PHP quite frustrating. Quite often I will try and run the script and just get a blank screen back. No error message, just 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?.

So, is there a way to get PHP to produce useful error message like Java does? Can anyone recommend good PHP debugging tips, tools and techniques?


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

29条回答 默认 最新

  • 已采纳
    csdnceshi64 游.程 2009-05-10 09:52

    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:

    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/

    点赞 21 评论 复制链接分享
  • weixin_41568208 北城已荒凉 2011-07-04 19:46

    The following enables all errors:

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

    Also see the following links

    点赞 23 评论 复制链接分享
  • csdnceshi66 必承其重 | 欲带皇冠 2009-05-10 09:54
    error_reporting(E_ALL | E_STRICT);

    And turn on display errors in php.ini

    点赞 10 评论 复制链接分享
  • csdnceshi80 胖鸭 2011-07-04 19:49

    If you are super cool, you might try:

    $test_server = $_SERVER['SERVER_NAME'] == "" || $_SERVER['SERVER_NAME'] == "localhost" || substr($_SERVER['SERVER_NAME'],0,3) == "192";

    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.

    点赞 10 评论 复制链接分享
  • csdnceshi73 喵-见缝插针 2014-08-19 15:36
    error_reporting(E_ALL | E_STRICT);
    ini_set('display_errors', 1);
    ini_set('html_errors', 1);

    In addition, you can get more detailed information with xdebug.

    点赞 10 评论 复制链接分享
  • csdnceshi54 hurriedly% 2009-05-10 18:16

    Aside from error_reporting and the display_errors ini setting, you can get SYNTAX errors from your web server's log files. When I'm developing PHP I load my development system's web server logs into my editor. Whenever I test a page and get a blank screen, the log file goes stale and my editor asks if I want to reload it. When I do, I jump to the bottom and there is the syntax error. For example:

    [Sun Apr 19 19:09:11 2009] [error] [client] PHP Parse error:  syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in D:\\webroot\\test\\test.php on line 9
    点赞 9 评论 复制链接分享
  • csdnceshi60 ℡Wang Yan 2012-11-25 13:30

    Try this PHP error reporting reference tool. It's a very good visual reference and helped me understand the complex error reporting mechanism.

    点赞 9 评论 复制链接分享
  • csdnceshi68 local-host 2015-07-15 22:38

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

    Nette Tracy screenshot

    点赞 9 评论 复制链接分享
  • csdnceshi51 旧行李 2009-05-10 09:54

    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);
    点赞 8 评论 复制链接分享
  • csdnceshi65 larry*wei 2009-05-10 09:54

    You can include the following lines in the file you want to debug:

    ini_set('display_errors', '1');

    This overrides the default settings in php.ini, which just make PHP report the errors to the log.

    点赞 8 评论 复制链接分享
  • weixin_41568126 乱世@小熊 2014-06-18 01:03

    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.

    点赞 8 评论 复制链接分享
  • weixin_41568127 ?yb? 2009-05-10 10:21

    FirePHP can be useful as well.

    点赞 7 评论 复制链接分享
  • csdnceshi72 谁还没个明天 2014-11-10 11:23

    if you are a ubuntu user then goto your terminal and run this command

    sudo tail -50f /var/log/apache2/error.log

    where it will display recent 50 errors. There is a error file error.log for apache2 which logs all the errors.

    点赞 7 评论 复制链接分享
  • csdnceshi65 larry*wei 2017-12-04 20:54
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    点赞 7 评论 复制链接分享
  • weixin_41568183 零零乙 2011-07-04 19:54

    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
    点赞 6 评论 复制链接分享
  • weixin_41568196 撒拉嘿哟木头 2013-01-24 15:06

    For quick, hands-on troubleshooting I normally suggest here on SO:

    error_reporting(~0); ini_set('display_errors', 1);

    to be put at the beginning of the script that is under trouble-shooting. This is not perfect, the perfect variant is that you also enable that in the php.ini and that you log the errors in PHP to catch syntax and startup errors.

    The settings outlined here display all errors, notices and warnings, including strict ones, regardless which PHP version.

    Next things to consider:

    • Install Xdebug and enable remote-debugging with your IDE.

    See as well:

    点赞 6 评论 复制链接分享
  • weixin_41568131 10.24 2009-05-10 09:58

    You can enable full error reporting (including notices and strict messages). Some people find this too verbose, but it's worth a try. Set error_reporting to E_ALL | E_STRICT in your php.ini.

    error_reporting = E_ALL | E_STRICT

    E_STRICT will notify you about deprecated functions and give you recommendations about the best methods to do certain tasks.

    If you don't want notices, but you find other message types helpful, try excluding notices:

    error_reporting = (E_ALL | E_STRICT) & ~E_NOTICE

    Also make sure that display_errors is enabled in php.ini. If your PHP version is older than 5.2.4, set it to On:

    display_errors = "On"

    If your version is 5.2.4 or newer, use:

    display_errors = "stderr"
    点赞 5 评论 复制链接分享
  • weixin_41568127 ?yb? 2014-02-01 06:24

    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:

    ini_set("display_errors", 1);

    Another way to use debugger tools like xdebug in your IDE.

    点赞 4 评论 复制链接分享
  • weixin_41568134 MAO-EYE 2014-06-10 13:37

    The two key lines you need to get useful errors out of PHP are:


    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):

        // local
        case 'yourdomain.dev':
        // dev
        case 'dev.yourdomain.com':
        case 'yourdomain.com':
    点赞 4 评论 复制链接分享
  • csdnceshi51 旧行李 2009-05-10 09:59

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

    点赞 3 评论 复制链接分享
  • csdnceshi50 三生石@ 2009-05-10 12:09

    To turn on full error reporting, add this to your script:


    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.

    点赞 2 评论 复制链接分享
  • csdnceshi65 larry*wei 2014-02-02 20:47

    PHP Configuration

    2 entries in php.ini dictate the output of errors:

    1. display_errors
    2. error_reporting

    In production, display_errors is usually set to Off (Which is a good thing, because error display in production sites is generally not desirable!).

    However, in development, it should be set to On, so that errors get displayed. Check!

    error_reporting (as of PHP 5.3) is set by default to E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED (meaning, everything is shown except for notices, strict standards and deprecation notices). When in doubt, set it to E_ALL to display all the errors. Check!

    Whoa whoa! No check! I can't change my php.ini!

    That's a shame. Usually shared hosts do not allow the alteration of their php.ini file, and so, that option is sadly unavailable. But fear not! We have other options!

    Runtime configuration

    In the desired script, we can alter the php.ini entries in runtime! Meaning, it'll run when the script runs! Sweet!

    ini_set("display_errors", "On");

    These two lines will do the same effect as altering the php.ini entries as above! Awesome!

    I still get a blank page/500 error!

    That means that the script hadn't even run! That usually happens when you have a syntax error!

    With syntax errors, the script doesn't even get to runtime. It fails at compile time, meaning that it'll use the values in php.ini, which if you hadn't changed, may not allow the display of errors.

    Error logs

    In addition, PHP by default logs errors. In shared hosting, it may be in a dedicated folder or on the same folder as the offending script.

    If you have access to php.ini, you can find it under the error_log entry.

    点赞 2 评论 复制链接分享
  • csdnceshi67 bug^君 2013-05-06 14:14

    On the top of the page choose a parameter

    error_reporting(E_ERROR | E_WARNING | E_PARSE);
    点赞 1 评论 复制链接分享
  • csdnceshi60 ℡Wang Yan 2013-06-15 11:55

    Turning on error reporting is the correct solution, however it does not seem to take effect in the program that turns it on, but only in subsequently included programs.

    Thus, I always create a file/program (which I usually call "genwrap.php") which has essentially the same code as the popular solution here (ie. turn on error reporting) and it also then includes the page I actually want to call.

    There are 2 steps to implement this debugging;

    One - create genwrap.php and put this code in it:

    ini_set('display_errors', 'On');

    Two - change the link to the program/page you want to debug to go via genwrap.php,

    Eg: change:

    $.ajax('dir/pgm.php?param=val').done(function(data) { /* ... */


    $.ajax('dir/genwrap.php?page=pgm.php&param=val').done(function(data) { /* ... */
    点赞 评论 复制链接分享
  • weixin_41568110 七度&光 2013-07-09 18:05

    My usual problem are "little, stupid" parser errors which unfortunately do not show up.

    However, when a .PHP-File includes a file that has parser-errors, they are shown! So I had the idea of writing a little "executor-script" that is launched with the name of the buggy file as argument, i.e. example.com/sx.php?sc=buggy.php

    It had already saved me from a lot of headache, maybe it will be helpful to someone else, too :)


    $sc = $_GET["sc"];
    if ((!isset($_GET["sc"]) && empty($_GET["sc"]))) {
        echo "Please select file to execute using ?sc= (you may omit the .PHP-extension)";
    } else {
        $sc = $_GET["sc"];
        if (false==stripos('.php',$sc)) $sc.='.php';  // adjust this if your preferred extension is php5!
    点赞 评论 复制链接分享
  • csdnceshi55 ~Onlooker 2014-10-01 19:48

    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.

    点赞 评论 复制链接分享
  • csdnceshi69 YaoRaoLov 2015-03-07 18:16

    In addition to the very many excellent answers above you could also implement the following two functions in your projects. They will catch every non-syntax error before application/script exit. Inside the functions you can do a backtrace and log or render a pleasant 'Site is under maintenance' message to the public.

    Fatal Errors:









    点赞 评论 复制链接分享
  • csdnceshi75 衫裤跑路 2016-06-29 14:59

    Use Kint. It is combination of debugging commands on steroids. https://kint-php.github.io/kint/ It is very similar to Nette Tracy

    点赞 评论 复制链接分享
  • csdnceshi70 笑故挽风 2018-09-14 03:31

    In addition to all the wonderful answers here, I'd like to throw in a special mention for the MySQLi and PDO libraries.

    In order to...

    1. Always see database related errors, and
    2. Avoid checking the return types for methods to see if something went wrong

    The best option is to configure the libraries to throw exceptions.


    Add this near the top of your script


    This is best placed before you use new mysqli() or mysqli_connect().


    Set the PDO::ATTR_ERRMODE attribute to PDO::ERRMODE_EXCEPTION on your connection instance. You can either do this in the constructor

    $pdo = new PDO('driver:host=localhost;...', 'username', 'password', [

    or after creation

    点赞 评论 复制链接分享