You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改PHP错误输出样式?如何将PHP错误以表格形式展示?

Styling PHP Errors into a Table Format (for MAMP 4.2 + PHP 7.1.6)

Hey there! I totally get it—plain text errors are dull and hard to parse quickly. Let’s turn them into that clean, orange-themed table you’re aiming for. Here’s a step-by-step guide tailored to your setup:

1. Override PHP’s Default Error Handling

PHP lets you replace its generic error output with your own using set_error_handler(). We’ll also add a shutdown handler to catch fatal errors (since set_error_handler() misses some critical ones like E_ERROR).

Drop this code at the top of your project’s main entry file (like index.php):

// Enable error display and disable default HTML formatting
ini_set('display_errors', 1);
ini_set('html_errors', 0);

// Register our custom error handler
set_error_handler('customErrorHandler');

// Catch fatal errors that slip past the regular handler
register_shutdown_function('customFatalErrorHandler');

2. Build the Custom Table-Style Error Handler

Now let’s write the function that generates the styled error table, with orange-themed styling to match the example you referenced:

function customErrorHandler($errno, $errstr, $errfile, $errline) {
    // Map error levels to labels and accent colors
    $errorLevelMap = [
        E_ERROR => ['label' => 'Fatal Error', 'color' => '#dc2626'],
        E_WARNING => ['label' => 'Warning', 'color' => '#f97316'],
        E_NOTICE => ['label' => 'Notice', 'color' => '#f59e0b'],
        E_USER_ERROR => ['label' => 'User Error', 'color' => '#dc2626'],
        E_USER_WARNING => ['label' => 'User Warning', 'color' => '#f97316'],
        E_USER_NOTICE => ['label' => 'User Notice', 'color' => '#f59e0b'],
    ];

    $currentLevel = isset($errorLevelMap[$errno]) ? $errorLevelMap[$errno] : ['label' => 'Unknown Error', 'color' => '#6b7280'];

    // Generate styled table HTML
    $errorOutput = '
    <style>
        .php-error-table {
            width: 90%;
            margin: 2rem auto;
            border-collapse: collapse;
            font-family: "Arial", sans-serif;
            box-shadow: 0 2px 8px rgba(0,0,0,0.15);
        }
        .php-error-table th {
            background-color: ' . $currentLevel['color'] . ';
            color: white;
            padding: 1rem;
            text-align: left;
            font-size: 1.1rem;
        }
        .php-error-table td {
            padding: 0.8rem;
            border-bottom: 1px solid #f3f4f6;
        }
        .php-error-table tr:nth-child(even) {
            background-color: #fff7ed;
        }
        .php-error-code {
            font-family: monospace;
            background-color: #fef2f2;
            padding: 0.2rem 0.5rem;
            border-radius: 3px;
        }
    </style>
    <table class="php-error-table">
        <tr>
            <th colspan="2">' . $currentLevel['label'] . '</th>
        </tr>
        <tr>
            <td width="160px"><strong>Error Message:</strong></td>
            <td>' . htmlspecialchars($errstr) . '</td>
        </tr>
        <tr>
            <td><strong>File:</strong></td>
            <td>' . htmlspecialchars($errfile) . '</td>
        </tr>
        <tr>
            <td><strong>Line:</strong></td>
            <td>' . $errline . '</td>
        </tr>
        <tr>
            <td><strong>Error Code:</strong></td>
            <td><span class="php-error-code">' . $errno . '</span></td>
        </tr>
    </table>';

    echo $errorOutput;
    return true; // Prevent PHP from showing default error
}

// Handle fatal errors that bypass the regular handler
function customFatalErrorHandler() {
    $lastError = error_get_last();
    if ($lastError && in_array($lastError['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR])) {
        customErrorHandler($lastError['type'], $lastError['message'], $lastError['file'], $lastError['line']);
    }
}

3. Tweak MAMP’s PHP Configuration

Make sure your MAMP settings allow error display:

  1. Open MAMP, go to Preferences > PHP and select your 7.1.6 version.
  2. Click Open php.ini to edit the configuration file.
  3. Find and update these lines:
    display_errors = On
    error_reporting = E_ALL
    
  4. Save the file and restart MAMP’s Apache server.

Quick Reminders

  • Only use this in development! For production, disable display_errors and log errors to a file instead (log_errors = On and error_log = /path/to/your/error.log).
  • Adjust the CSS in the <style> block to match your exact desired look—tweak colors, fonts, or padding as needed.

内容的提问来源于stack exchange,提问作者norixxx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:37:39