Matrix-style Lettering in Green

3-minute read

Improved PHP Error Reporting in ClassicPress

Learn how to quickly improve the readability of PHP’s error messages and boost your ClassicPress development speed! Even mile-long stack traces will be human-readable!

When writing PHP for ClassicPress plugins or themes, you should always enable error reporting. This allows you to catch and solve problems before releasing the code. The bad thing about error messages, though, is that they are sometimes a cryptic garble of text.

Like many, I use XAMPP as a local development environment (Apache server) and I have a tip for you that will help immensely! You can improve the readability of error messages with just a tiny bit of CSS – yes, CSS! Alright, take a quick look at the side-by-side image…then, let’s do this thing!

PHP Error Reporting Display
PHP Error Reporting Display, Before and After Formatting

Enable Debugging in ClassicPress

Before you can see errors on the screen, you’ll have to enable debugging mode in ClassicPress. This is achieved by making a simple change in the wp-config.php file. Do this first.

  1. In `wp-config.php`, find the line that reads define('WP_DEBUG', false);
  2. Change the `false` to true.
  3. Save the file.

That takes care of enabling error reporting for ClassicPress. Next, let’s take a look at the default display for error messages to get a baseline for comparison.

Default PHP Error Message Display

For the following screen capture, I’ve written PHP that generates one notice, one warning, and one fatal error. Since the notice and warning only span a single line, they are fairly easy to understand. However, the fatal error, which has produced a stack trace, is much harder to understand.

Default PHP Error Reporting Display
Default PHP Error Reporting Display

Improved PHP Error Message Display

The screen capture below shows vastly improved readability. Most notably, the stack trace is actually readable. So, how can you fix your error display? Glad you asked!

Enhanced PHP Error Reporting Display
Enhanced PHP Error Reporting Display

How to Apply CSS to the PHP Error Display

You will be adding your CSS to the php.ini file, so, the first thing to do is open it for editing. If you’re also using XAMPP, you can quickly locate and open the file using the XAMPP Control Panel.

Accessing php.ini in XAMPP Control Panel
Accessing php.ini in XAMPP Control Panel
  1. Open your `php.ini` file in any text editor.
  2. Search for `error_prepend_string`:
    The default value is something like <span style='color:#FF0000;'>. Change it to
    `<div style=’white-space:pre;font-family:monospace;font-size:14pt;color:#f00;’>`
  3. Search for `error_append_string`:
    The default value is a closing </span> tag. Change this value to closing `</div>` tag.
  4. Save the `php.ini` file.
  5. Stop and restart Apache.

Wrapping Up

It really is that simple. After adding the above tweaks to your php.ini file, debugging will be faster and easier. The error messages are enhanced with style and readability! Now, to satisfy the curiosity of anyone who might wonder, let me leave you with this:

XAMPP stands for cross-platform (X), Apache server (A), Maria Database (M), PHP (P), and Perl (P). Some people say XAMPP like “zamp” while others say it like “eggs-amp” – either way, they’re talking about the same thing.

What do you think?

Did you know PHP errors were so easy to reformat? How will you style your display? Or, are you a ninja that can grok a stack trace in your sleep?! I’d love to hear your thoughts – let me know in the comments!